<?php

namespace Modules\Catalog\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Modules\Catalog\Database\Factories\DepartureTimeSlotFactory;
use Modules\Routing\Models\EvRoute;

/**
 * A shared catalog of departure times, attached to routes via a pivot in the
 * Routing module — not owned by any single route.
 */
class DepartureTimeSlot extends Model
{
    /** @use HasFactory<DepartureTimeSlotFactory> */
    use HasFactory;

    /**
     * @var list<string>
     */
    protected $fillable = [
        'label',
        'time',
        'is_active',
    ];

    /**
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'time' => 'datetime:H:i',
            'is_active' => 'boolean',
        ];
    }

    public function routes(): BelongsToMany
    {
        return $this->belongsToMany(EvRoute::class, 'ev_route_time_slots')
            ->withPivot('is_active')
            ->withTimestamps();
    }
}
