<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;

class OrganisationTaxSettingBackup extends Model
{
    use HasFactory, SoftDeletes, LogsActivity;

    protected $fillable = [
        'original_id',
        'org_id',
        'salary_range',
        'percentage',
        'tax_year',
        'backup_version',
        'upgraded_by',
    ];

    protected $casts = [
        'salary_range' => 'decimal:2',
        'percentage' => 'decimal:2',
    ];

    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults()
            ->logAll()
            ->useLogName('TaxSettingBackup')
            ->logOnlyDirty()
            ->setDescriptionForEvent(fn(string $eventName) => "Tax Setting Backup '#{$this->id}' has been {$eventName}")
            ->dontSubmitEmptyLogs();
    }

    // Relationships
    public function organisation()
    {
        return $this->belongsTo(Organisation::class, 'org_id');
    }

    public function upgradedBy()
    {
        return $this->belongsTo(User::class, 'upgraded_by');
    }

    public function originalTaxSetting()
    {
        return $this->belongsTo(OrganisationTaxSetting::class, 'original_id');
    }
}
