<?php

namespace App\Models;

use Database\Factories\UserFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    /** @use HasFactory<UserFactory> */
    use HasFactory, Notifiable;

    protected $fillable = [
        'staff_id', 'name', 'email', 'password', 'role', 'phone', 'gender', 'date_of_birth',
        'address', 'province', 'postal_code', 'sin_last4', 'tax_province', 'work_authorization_status',
        'emergency_contact', 'department_id', 'designation_id', 'job_title',
        'employment_date', 'employment_status', 'basic_salary', 'payroll_frequency',
        'bank_name', 'bank_institution_number', 'bank_transit_number', 'bank_account',
        'profile_photo', 'last_login_at',
    ];

    protected $hidden = ['password', 'remember_token'];

    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'password' => 'hashed',
            'date_of_birth' => 'date',
            'employment_date' => 'date',
            'last_login_at' => 'datetime',
            'basic_salary' => 'decimal:2',
        ];
    }

    public function isAdmin(): bool
    {
        return $this->role === 'admin';
    }

    public function department()
    {
        return $this->belongsTo(Department::class);
    }

    public function designation()
    {
        return $this->belongsTo(Designation::class);
    }

    public function documents()
    {
        return $this->hasMany(EmployeeDocument::class);
    }

    public function attendanceRecords()
    {
        return $this->hasMany(AttendanceRecord::class);
    }

    public function leaveRequests()
    {
        return $this->hasMany(LeaveRequest::class);
    }

    public function payrolls()
    {
        return $this->hasMany(Payroll::class);
    }

    public function performanceReviews()
    {
        return $this->hasMany(PerformanceReview::class);
    }

    public function tickets()
    {
        return $this->hasMany(HrTicket::class);
    }

    public function trainings()
    {
        return $this->belongsToMany(Training::class, 'training_enrollments')->withPivot(['status', 'completed_at', 'notes'])->withTimestamps();
    }
}
