<?php

namespace App\Models;

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

class Employee extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'user_id', 'first_name', 'last_name', 'middle_name',
        'present_address', 'permanent_address', 'date_of_birth', 'place_of_birth',
        'citizenship', 'gender', 'marital_status', 'children_info',
        'physical_limitation', 'national_id', 'passport_number',
        'position', 'employee_number', 'department_id', 'date_of_employment',
        'employment_type', 'status', 'salary_usd', 'salary_lrd', 'salary_other_source',
        'bank_name', 'bank_account', 'special_skills', 'professional_memberships',
        'relatives_at_ul', 'publications', 'scholastic_honors', 'notes',
        'emergency_contact_name', 'emergency_contact_phone', 'emergency_contact_relation',
    ];

    protected $casts = [
        'date_of_birth'      => 'date',
        'date_of_employment' => 'date',
        'salary_usd'         => 'decimal:2',
        'salary_lrd'         => 'decimal:2',
    ];

    public function getFullNameAttribute(): string
    {
        return trim("{$this->last_name}, {$this->first_name} " . ($this->middle_name ?? ''));
    }

    public function getAgeAttribute(): ?int
    {
        return $this->date_of_birth ? $this->date_of_birth->age : null;
    }

    public function getYearsOfServiceAttribute(): ?int
    {
        return $this->date_of_employment ? $this->date_of_employment->diffInYears(now()) : null;
    }

    public function getStatusBadgeAttribute(): string
    {
        return match($this->status) {
            'active'     => 'success',
            'on_leave'   => 'warning',
            'suspended'  => 'danger',
            'terminated' => 'dark',
            'retired'    => 'secondary',
            default      => 'info',
        };
    }

    // Relationships
    public function user()              { return $this->belongsTo(User::class); }
    public function department()        { return $this->belongsTo(Department::class); }
    public function education()         { return $this->hasMany(EmployeeEducation::class); }
    public function employmentHistory() { return $this->hasMany(EmployeeEmploymentHistory::class); }
    public function references()        { return $this->hasMany(EmployeeReference::class); }
    public function documents()         { return $this->hasMany(EmployeeDocument::class); }
    public function leaves()            { return $this->hasMany(Leave::class); }
    public function leaveBalances()     { return $this->hasMany(LeaveBalance::class); }
    public function evaluations()       { return $this->hasMany(PerformanceEvaluation::class); }
    public function disciplineRecords() { return $this->hasMany(DisciplineRecord::class); }
    public function attendance()        { return $this->hasMany(Attendance::class); }
    public function payrollRecords()    { return $this->hasMany(PayrollRecord::class); }
    public function trainings()         { return $this->belongsToMany(Training::class, 'training_participants')->withPivot('status', 'score', 'certificate_issued')->withTimestamps(); }

    // Scopes
    public function scopeActive($query)      { return $query->where('status', 'active'); }
    public function scopeByDepartment($q, $d){ return $q->where('department_id', $d); }
}
