<?php

namespace App\Models;

use App\Enums\LeaveRequestStatus;
use App\Models\Concerns\HasRejection;
use App\Models\Concerns\InteractsWithActivityLog;
use Illuminate\Database\Eloquent\Attributes\Appends;
use Illuminate\Database\Eloquent\Attributes\Guarded;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

#[Guarded(['id'])]
#[Appends([
    'can_verify',
    'created_at_formatted',
    'employee_name',
    'end_date_formatted',
    'end_date_input',
    'is_editable',
    'rejection_reason',
    'start_date_formatted',
    'start_date_input',
    'status_label',
])]
class LeaveRequest extends Model
{
    // 1. Use Trait
    use HasFactory, HasRejection, InteractsWithActivityLog;

    // 2. Casting
    protected function casts(): array
    {
        return [
            'end_date' => 'date',
            'start_date' => 'date',
            'status' => LeaveRequestStatus::class,
            'total_days' => 'integer',
            'verified_at' => 'datetime',
        ];
    }

    // 3. Scope (grouped by column, then alphabetical)
    // Column Group: status
    #[Scope]
    protected function approved(Builder $query): void
    {
        $query->where('status', LeaveRequestStatus::APPROVED);
    }

    #[Scope]
    protected function pending(Builder $query): void
    {
        $query->where('status', LeaveRequestStatus::PENDING);
    }

    #[Scope]
    protected function rejected(Builder $query): void
    {
        $query->where('status', LeaveRequestStatus::REJECTED);
    }

    // 4. Attribute
    public function canVerify(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->status === LeaveRequestStatus::PENDING,
        );
    }

    public function createdAtFormatted(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->created_at?->translatedFormat('l, d F Y H:i'),
        );
    }

    public function employeeName(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->employee?->user?->profile?->full_name
                ?? $this->employee?->user?->username,
        );
    }

    public function endDateFormatted(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->end_date?->translatedFormat('l, d F Y'),
        );
    }

    public function endDateInput(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->end_date?->format('Y-m-d'),
        );
    }

    public function isEditable(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->status === LeaveRequestStatus::PENDING,
        );
    }

    public function rejectionReason(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->rejection?->reason,
        );
    }

    public function startDateFormatted(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->start_date?->translatedFormat('l, d F Y'),
        );
    }

    public function startDateInput(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->start_date?->format('Y-m-d'),
        );
    }

    public function statusLabel(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->status?->label(),
        );
    }

    // 5. Relation
    public function employee(): BelongsTo
    {
        return $this->belongsTo(Employee::class);
    }

    public function verifiedBy(): BelongsTo
    {
        return $this->belongsTo(User::class, 'verified_by_id');
    }
}
