<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Horario base recurrente.
     * - Si employee_id es null y branch_id está → horario general de la sede.
     * - Si employee_id está → horario individual del empleado (override).
     * weekday: 0 = Domingo ... 6 = Sábado.
     * Permite múltiples filas por día (ej: 8-12 y 14-18 con descanso).
     */
    public function up(): void
    {
        Schema::create('schedules', function (Blueprint $table) {
            $table->id();
            $table->foreignId('company_id')->constrained()->cascadeOnDelete();
            $table->foreignId('branch_id')->nullable()->constrained()->cascadeOnDelete();
            $table->foreignId('employee_id')->nullable()->constrained()->cascadeOnDelete();

            $table->unsignedTinyInteger('weekday');     // 0-6
            $table->time('start_time');
            $table->time('end_time');
            $table->boolean('is_active')->default(true);

            $table->timestamps();

            $table->index(['company_id', 'employee_id', 'weekday']);
            $table->index(['company_id', 'branch_id', 'weekday']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('schedules');
    }
};
