<?php

namespace App\Models;

use App\Models\Concerns\BelongsToTenant;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class Dentist extends Model
{
    use BelongsToTenant, HasFactory, SoftDeletes;

    protected $fillable = [
        'tenant_id', 'user_id', 'name', 'specialty', 'photo_path', 'color',
        'slot_duration_min', 'buffer_min', 'is_bookable_online', 'status',
    ];

    protected function casts(): array
    {
        return ['is_bookable_online' => 'boolean'];
    }

    public function schedules(): HasMany
    {
        return $this->hasMany(DentistSchedule::class)->orderBy('weekday')->orderBy('start_time');
    }

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

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

    public function scopeActive($query)
    {
        return $query->where('status', 'active');
    }

    /** Slot length including the buffer that follows it. */
    public function stepMinutes(): int
    {
        return $this->slot_duration_min + $this->buffer_min;
    }
}
