<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ClearanceStatusLog extends Model
{
    use HasFactory;

    protected $fillable = [
        'clearance_id',
        'old_status',
        'new_status',
        'action',
        'reason',
        'changed_by',
        'changed_at'
    ];

    protected $casts = [
        'changed_at' => 'datetime'
    ];

    /**
     * Get the clearance associated with this log
     */
    public function clearance()
    {
        return $this->belongsTo(Clearance::class, 'clearance_id');
    }

    /**
     * Get the user who made the change
     */
    public function changedBy()
    {
        return $this->belongsTo(User::class, 'changed_by');
    }

    /**
     * Get old status name
     */
    public function getOldStatusNameAttribute()
    {
        return $this->getStatusName($this->old_status);
    }

    /**
     * Get new status name
     */
    public function getNewStatusNameAttribute()
    {
        return $this->getStatusName($this->new_status);
    }

    /**
     * Helper function to get status name
     */
    private function getStatusName($status)
    {
        $statusNames = [
            0 => 'FOR ROUTING',
            1 => 'PENDING CLEARANCE',
            2 => 'LASTPAY COMPUTATION',
            3 => 'EMPLOYEE APPROVAL',
            4 => 'FOR ENDORSEMENT',
            5 => 'FOR FINAL APPROVAL',
            6 => 'CHECK REQUEST',
            7 => 'CHECK RELEASING',
            8 => 'DOCUMENTATION',
            9 => 'SCHEDULE OF RELEASE',
            10 => 'READY RELEASE',
            11 => 'COMPLETED',
            99 => 'DEFERRED'
        ];

        return $statusNames[$status] ?? 'UNKNOWN';
    }
}