<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

class Employee extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id',
        'employee_code',
        'biotime_emp_code',
        'first_name',
        'last_name',
        'email',
        'phone',
        'gender',
        'date_of_birth',
        'department_id',
        'designation_id',
        'area_id',
        'hire_date',
        'status',
        'iqama_number',
        'passport_number',
        'iqama_expiry_date',
        'nationality',
        'joining_date',
        'resigning_date',
        'employment_status',
        'employment_type',
        'workflow_role',
        'local_name',
        'contact_tel',
        'office_tel',
        'city',
        'postcode',
        'manager_id',
        'loan_approval_authority',
        'address',
        'profile_picture',
        'device_access_role',
        'device_privilege',
        'default_shift_id',
        'grace_period_override',
        'app_language',
        'notifications_enabled',
        'bank_name',
        'bank_account',
        'iban',
        'pay_grade',
        'leave_group_id',
    ];

    protected $casts = [
        'date_of_birth' => 'date',
        'hire_date' => 'date',
        'iqama_expiry_date' => 'date',
        'joining_date' => 'date',
        'resigning_date' => 'date',
        'notifications_enabled' => 'boolean',
    ];

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

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

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

    public function manager(): BelongsTo
    {
        return $this->belongsTo(Employee::class, 'manager_id');
    }

    public function subordinates(): HasMany
    {
        return $this->hasMany(Employee::class, 'manager_id');
    }

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

    public function bioTimeAttendanceLogs(): HasMany
    {
        return $this->hasMany(BioTimeAttendanceLog::class);
    }

    public function dailyAttendances(): HasMany
    {
        return $this->hasMany(DailyAttendance::class);
    }

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

    public function salaryStructure(): HasOne
    {
        return $this->hasOne(SalaryStructure::class);
    }

    public function payslips(): HasMany
    {
        return $this->hasMany(Payslip::class);
    }

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