<?php

namespace App\Domain\HR\Models;

use App\Domain\HR\Enums\PayslipStatus;
use App\Domain\HR\Enums\StaffPaymentMethod;
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;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Domain\Shared\Traits\BelongsToBranch;

class Payslip extends Model
{
    use BelongsToAcademy, HasUuid, SoftDeletes, BelongsToBranch;

    protected $fillable = [
        'academy_id', 'branch_id',
        'trainer_id',
        'payroll_period_id',
        'payslip_number',
        'base_amount',
        'session_earnings',
        'bonuses',
        'penalties',
        'advances_deducted',
        'tax_amount',
        'insurance_amount',
        'other_deductions',
        'gross_amount',
        'total_deductions',
        'net_amount',
        'status',
        'approved_by',
        'approved_at',
        'paid_at',
        'payment_method',
        'payment_reference',
        'notes',
        'metadata',
        'created_by',
    ];

    protected $casts = [
        'status' => PayslipStatus::class,
        'payment_method' => StaffPaymentMethod::class,
        'base_amount' => 'integer',
        'session_earnings' => 'integer',
        'bonuses' => 'integer',
        'penalties' => 'integer',
        'advances_deducted' => 'integer',
        'tax_amount' => 'integer',
        'insurance_amount' => 'integer',
        'other_deductions' => 'integer',
        'gross_amount' => 'integer',
        'total_deductions' => 'integer',
        'net_amount' => 'integer',
        'approved_at' => 'datetime',
        'paid_at' => 'datetime',
        'metadata' => 'array',
    ];

    // --- Relationships ---

    public function trainer(): BelongsTo
    {
        return $this->belongsTo(Trainer::class);
    }

    public function period(): BelongsTo
    {
        return $this->belongsTo(PayrollPeriod::class, 'payroll_period_id');
    }

    public function items(): HasMany
    {
        return $this->hasMany(PayslipItem::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');
    }
}
