<?php

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

/**
 * SIFA P3-T7 — per-doctor schedule exceptions (spec §7 provider_schedule.exceptions).
 *
 * One row = one exception on a specific calendar date for a doctor:
 *   - unavailable : the doctor is off; the availability engine drops every slot
 *                   that day (or the given window when start/end supplied).
 *   - modified    : the working window for that date is overridden by
 *                   start_time / end_time.
 *   - extra       : an additional working window (start_time / end_time) is
 *                   added on top of the regular pattern.
 *
 * business_id-scoped. created_by is the acting user.
 */
class CreateClcScheduleExceptionsTable extends Migration
{
    public function up()
    {
        if (Schema::hasTable('clc_schedule_exceptions')) return;

        Schema::create('clc_schedule_exceptions', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('business_id');
            $table->unsignedInteger('doctor_id');
            $table->date('date');
            $table->enum('type', ['unavailable', 'modified', 'extra'])->default('unavailable');
            $table->time('start_time')->nullable();   // for modified / extra (and partial unavailable)
            $table->time('end_time')->nullable();
            $table->string('reason')->nullable();
            $table->unsignedInteger('created_by')->nullable();
            $table->timestamps();

            $table->foreign('business_id')->references('id')->on('business')->onDelete('cascade');
            $table->foreign('doctor_id')->references('id')->on('clc_doctors')->onDelete('cascade');
            $table->index(['business_id', 'doctor_id', 'date']);
        });
    }

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