<?php

namespace App\Models;

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

class LeaveType extends Model
{
    use HasFactory, \App\Traits\HotelScopeTrait;

    protected $fillable = [
        'hotel_id',
        'name',
        'code',
        'description',
        'max_days_per_year',
        'max_consecutive_days',
        'carry_forward_enabled',
        'max_carry_forward_days',
        'requires_approval',
        'approval_level',
        'gender_specific',
        'gender',
        'min_service_days',
        'max_balance',
        'encashment_enabled',
        'encashment_percentage',
        'is_active',
        'color'
    ];

    protected $casts = [
        'max_days_per_year' => 'integer',
        'max_consecutive_days' => 'integer',
        'carry_forward_enabled' => 'boolean',
        'max_carry_forward_days' => 'integer',
        'requires_approval' => 'boolean',
        'min_service_days' => 'integer',
        'max_balance' => 'integer',
        'encashment_enabled' => 'boolean',
        'encashment_percentage' => 'decimal:2',
        'is_active' => 'boolean',
    ];

    /**
     * Get the leaves for this type
     */
    public function leaves(): HasMany
    {
        return $this->hasMany(Leave::class);
    }

    /**
     * Get active leave types
     */
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    /**
     * Get leave type badge color
     */
    public function getBadgeColorAttribute(): string
    {
        if ($this->color) {
            return $this->color;
        }

        $defaultColors = [
            'annual' => 'info',
            'sick' => 'danger',
            'casual' => 'warning',
            'maternity' => 'success',
            'paternity' => 'primary',
            'unpaid' => 'secondary',
            'compensatory' => 'dark'
        ];

        return $defaultColors[strtolower($this->code)] ?? 'secondary';
    }
}