<?php

namespace App\Models;

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

class Payroll extends Model
{
    use HasFactory;
    // protected $with = ['employee_data_count'];

    protected $guarded = [];

    protected $casts = [
        'payrollItems' => 'json',
        'payrollStart' => 'json',

    ];


    use LogsActivity;

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


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

    public function employee_data(): HasMany
    {
        return $this->hasMany(PayrollEmployee::class, "payroll_id");
    }

    public function employee_data_count()
    {
        return $this->hasMany(PayrollEmployee::class, "payroll_id")->count();
    }

    public function employeeDeduction()
    {
        return $this->hasMany(EmployeeDeduction::class);
    }
}
