<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Traits\Auditable;
use Carbon\Carbon;

class DoctorScheduleException extends Model
{
    use Auditable;

    protected $fillable = [
        'doctor_id',
        'exception_date',
        'type',
        'start_time',
        'end_time',
        'reason'
    ];

    protected $casts = [
        'exception_date' => 'date',
        'start_time' => 'datetime',
        'end_time' => 'datetime'
    ];

    public function doctor()
    {
        return $this->belongsTo(User::class, 'doctor_id');
    }

    /**
     * Verificar si hay una excepción para una fecha específica
     */
    public static function hasException($doctorId, $date)
    {
        $date = Carbon::parse($date)->toDateString();
        return self::where('doctor_id', $doctorId)
            ->whereDate('exception_date', $date)
            ->exists();
    }

    /**
     * Obtener excepciones para un rango de fechas
     */
    public static function getExceptionsForRange($doctorId, $startDate, $endDate)
    {
        return self::where('doctor_id', $doctorId)
            ->whereBetween('exception_date', [$startDate, $endDate])
            ->get();
    }
} 