<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Leave extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'leave_number', 'employee_id', 'leave_type_id', 'date_of_request',
        'date_from', 'date_to', 'duration_weeks', 'duration_months', 'total_days',
        'forwarding_address', 'duty_arrangement', 'reason', 'status',
        'dept_head_id', 'dept_approved_at', 'dept_comments',
        'hr_approver_id', 'hr_approved_at', 'hr_comments',
        'president_approver_id', 'president_approved_at', 'president_comments',
        'rejection_reason',
    ];

    protected $casts = [
        'date_of_request'     => 'date',
        'date_from'           => 'date',
        'date_to'             => 'date',
        'dept_approved_at'    => 'datetime',
        'hr_approved_at'      => 'datetime',
        'president_approved_at' => 'datetime',
    ];

    protected static function booted()
    {
        static::creating(function ($leave) {
            if (!$leave->leave_number) {
                $leave->leave_number = 'LV-' . date('Y') . '-' . str_pad(
                    self::whereYear('created_at', date('Y'))->count() + 1, 4, '0', STR_PAD_LEFT
                );
            }
            if (!$leave->total_days && $leave->date_from && $leave->date_to) {
                $leave->total_days = $leave->date_from->diffInWeekdays($leave->date_to) + 1;
            }
        });
    }

    public function getStatusBadgeAttribute(): string
    {
        return match($this->status) {
            'pending'       => 'warning',
            'dept_approved' => 'info',
            'hr_approved'   => 'primary',
            'approved'      => 'success',
            'rejected'      => 'danger',
            'cancelled'     => 'secondary',
            default         => 'light',
        };
    }

    public function getStatusLabelAttribute(): string
    {
        return match($this->status) {
            'pending'       => 'Pending',
            'dept_approved' => 'Dept. Approved',
            'hr_approved'   => 'HR Approved',
            'approved'      => 'Approved',
            'rejected'      => 'Rejected',
            'cancelled'     => 'Cancelled',
            default         => ucfirst($this->status),
        };
    }

    public function employee()   { return $this->belongsTo(Employee::class); }
    public function leaveType()  { return $this->belongsTo(LeaveType::class); }
    public function deptHead()   { return $this->belongsTo(User::class, 'dept_head_id'); }
    public function hrApprover() { return $this->belongsTo(User::class, 'hr_approver_id'); }
    public function president()  { return $this->belongsTo(User::class, 'president_approver_id'); }

    public function scopePending($q)  { return $q->where('status', 'pending'); }
    public function scopeApproved($q) { return $q->where('status', 'approved'); }
    public function scopeThisYear($q) { return $q->whereYear('date_from', date('Y')); }
}
