<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('doctor_location_assignments', function (Blueprint $table) {
            $table->id();
            $table->foreignId('organization_id')->constrained()->cascadeOnDelete();
            $table->foreignId('doctor_id')->constrained()->cascadeOnDelete();
            $table->foreignId('location_id')->constrained()->cascadeOnDelete();
            $table->decimal('consultation_fee', 12, 2)->default(0);
            $table->decimal('followup_fee', 12, 2)->default(0);
            $table->unsignedSmallInteger('slot_minutes')->default(20);
            $table->unsignedSmallInteger('booking_window_days')->default(30);
            $table->unsignedInteger('cancellation_cutoff_minutes')->default(120);
            $table->boolean('is_active')->default(true)->index();
            $table->timestamps();

            $table->unique(['doctor_id', 'location_id']);
            $table->index(['organization_id', 'is_active']);
        });

        Schema::create('doctor_working_hours', function (Blueprint $table) {
            $table->id();
            $table->foreignId('organization_id')->constrained()->cascadeOnDelete();
            $table->foreignId('doctor_location_assignment_id')->constrained()->cascadeOnDelete();
            $table->unsignedTinyInteger('weekday'); // ISO: 1 Monday - 7 Sunday
            $table->time('start_time');
            $table->time('end_time');
            $table->boolean('is_active')->default(true);
            $table->timestamps();

            $table->index(['doctor_location_assignment_id', 'weekday', 'is_active'], 'working_hours_lookup');
        });

        Schema::create('doctor_schedule_exceptions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('organization_id')->constrained()->cascadeOnDelete();
            $table->foreignId('doctor_location_assignment_id')->constrained()->cascadeOnDelete();
            $table->enum('type', ['leave', 'holiday', 'blocked_time', 'special_hours']);
            $table->dateTime('starts_at');
            $table->dateTime('ends_at');
            $table->string('reason', 255)->nullable();
            $table->foreignId('created_by_user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->index(['doctor_location_assignment_id', 'starts_at', 'ends_at'], 'schedule_exception_lookup');
        });

        Schema::create('assistant_assignments', function (Blueprint $table) {
            $table->id();
            $table->foreignId('organization_id')->constrained()->cascadeOnDelete();
            $table->foreignId('assistant_id')->constrained()->cascadeOnDelete();
            $table->foreignId('doctor_id')->constrained()->cascadeOnDelete();
            $table->foreignId('location_id')->constrained()->cascadeOnDelete();
            $table->boolean('is_active')->default(true);
            $table->boolean('can_create_patient')->default(true);
            $table->boolean('can_book_appointment')->default(true);
            $table->boolean('can_check_in')->default(true);
            $table->boolean('can_reschedule')->default(true);
            $table->boolean('can_cancel')->default(true);
            $table->boolean('can_edit_patient')->default(true);
            $table->boolean('can_collect_payment')->default(false);
            $table->timestamps();

            $table->unique(['assistant_id', 'doctor_id', 'location_id'], 'assistant_doctor_location_unique');
            $table->index(['organization_id', 'is_active']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('assistant_assignments');
        Schema::dropIfExists('doctor_schedule_exceptions');
        Schema::dropIfExists('doctor_working_hours');
        Schema::dropIfExists('doctor_location_assignments');
    }
};
