<?php

namespace Modules\ClinicCore\Entities;

use Illuminate\Database\Eloquent\Model;

/**
 * SIFA P3-T7 — a per-doctor schedule exception on a specific date.
 *
 * type:
 *   - unavailable : doctor off that day (or in the given window).
 *   - modified    : override the working window to start_time..end_time.
 *   - extra       : add an additional working window start_time..end_time.
 */
class ClcScheduleException extends Model
{
    protected $table = 'clc_schedule_exceptions';
    protected $guarded = ['id'];

    protected $casts = [
        'date' => 'date',
    ];

    public const TYPES = ['unavailable', 'modified', 'extra'];

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

    public function scopeForBusiness($query, $businessId)
    {
        return $query->where('business_id', $businessId);
    }

    /** The exception window as [HH:MM, HH:MM] when set, else null. */
    public function window(): ?array
    {
        if (empty($this->start_time) || empty($this->end_time)) {
            return null;
        }
        return [substr((string) $this->start_time, 0, 5), substr((string) $this->end_time, 0, 5)];
    }
}
