<?php

namespace App\Models;

use App\Models\Concerns\BelongsToCompany;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;

class LeaveType extends Model
{
    use BelongsToCompany;

    protected $table = 'leave_types';

    protected $fillable = [
        'company_id',
        'name',
        'short_code',
        'description',
        'is_paid',
        'total_leaves',
        'leave_count',
        'is_unlimited',
        'weekend_policy',
        'holiday_policy',
        'probation_allowed',
        'notice_period_allowed',
        'apply_to_all',
        'max_leaves_per_month',
        'count_type',
    ];

    protected $casts = [
        'is_paid' => 'boolean',
        'is_unlimited' => 'boolean',
        'probation_allowed' => 'boolean',
        'notice_period_allowed' => 'boolean',
        'apply_to_all' => 'boolean',
    ];

    public function leaves(): HasMany
    {
        return $this->hasMany(Leave::class, 'leave_type_id');
    }

    public function rule(): HasOne
    {
        return $this->hasOne(LeaveTypeRule::class, 'leave_type_id');
    }

    public function departments(): BelongsToMany
    {
        return $this->belongsToMany(Department::class, 'leave_type_departments', 'leave_type_id', 'department_id');
    }

    public function roles(): BelongsToMany
    {
        return $this->belongsToMany(Role::class, 'leave_type_roles', 'leave_type_id', 'role_id');
    }

    public function users(): BelongsToMany
    {
        return $this->belongsToMany(User::class, 'leave_type_users', 'leave_type_id', 'user_id');
    }
}
