<?php

namespace App\Domain\HR\Models;

use App\Domain\HR\Enums\PayrollPeriodStatus;
use App\Domain\Shared\Traits\BelongsToAcademy;
use App\Domain\Shared\Traits\HasUuid;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class PayrollPeriod extends Model
{
    use BelongsToAcademy, HasUuid;

    protected $fillable = [
        'academy_id',
        'period_start',
        'period_end',
        'status',
        'total_gross',
        'total_deductions',
        'total_net',
        'payslip_count',
        'approved_by',
        'approved_at',
        'closed_at',
        'notes',
        'created_by',
    ];

    protected $casts = [
        'status' => PayrollPeriodStatus::class,
        'period_start' => 'date',
        'period_end' => 'date',
        'total_gross' => 'integer',
        'total_deductions' => 'integer',
        'total_net' => 'integer',
        'payslip_count' => 'integer',
        'approved_at' => 'datetime',
        'closed_at' => 'datetime',
    ];

    // --- Relationships ---

    public function payslips(): HasMany
    {
        return $this->hasMany(Payslip::class);
    }

    public function approver(): BelongsTo
    {
        return $this->belongsTo(\App\Models\User::class, 'approved_by');
    }

    public function creator(): BelongsTo
    {
        return $this->belongsTo(\App\Models\User::class, 'created_by');
    }
}
