<?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('intakes', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('slug')->unique();
            $table->unsignedSmallInteger('year')->index();
            $table->timestamp('application_open_at')->nullable();
            $table->timestamp('application_close_at')->nullable();
            $table->date('start_date')->nullable();
            $table->enum('status', ['upcoming', 'open', 'closed', 'archived'])->default('upcoming')->index();
            $table->boolean('is_default')->default(false);
            $table->text('description')->nullable();
            $table->timestamps();
        });

        Schema::create('intake_programmes', function (Blueprint $table) {
            $table->id();
            $table->foreignId('intake_id')->constrained()->cascadeOnDelete();
            $table->foreignId('programme_id')->constrained()->cascadeOnDelete();
            $table->unsignedInteger('available_slots')->nullable();
            $table->timestamp('application_open')->nullable();
            $table->timestamp('application_close')->nullable();
            $table->decimal('application_fee_override', 15, 2)->nullable();
            $table->enum('status', ['active', 'inactive'])->default('active')->index();
            $table->timestamps();

            $table->unique(['intake_id', 'programme_id']);
        });
    }

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