<?php

namespace App\Modules\HR\Employees\Models;

use App\Models\User;
use App\Modules\HR\Employees\Enums\EmployeeDocumentType;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class EmployeeDocument extends Model
{
    protected $fillable = [
        'employee_id',
        'type',
        'file_path',
        'expires_at',
        'uploaded_by',
        'category',
        'status',
        'version',
        'replaces_document_id',
    ];

    protected function casts(): array
    {
        return [
            'type' => EmployeeDocumentType::class,
            'expires_at' => 'date',
        ];
    }

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

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

    public function replacedDocument(): BelongsTo
    {
        return $this->belongsTo(self::class, 'replaces_document_id');
    }

    public function effectiveStatus(): string
    {
        if ($this->status === 'active' && $this->expires_at?->isPast()) {
            return 'expired';
        }

        return $this->status;
    }
}
