<?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('programmes', function (Blueprint $table) {
            $table->id();
            $table->foreignId('school_id')->constrained()->cascadeOnDelete();
            $table->foreignId('department_id')->nullable()->constrained()->nullOnDelete();
            $table->string('name');
            $table->string('slug')->unique();
            $table->string('code', 40)->unique();
            $table->string('award');                       // e.g. Bachelor of Science
            $table->string('level', 40)->index();          // undergraduate|diploma|certificate|postgraduate
            $table->text('description')->nullable();
            $table->longText('overview')->nullable();
            $table->longText('career_opportunities')->nullable();
            $table->unsignedSmallInteger('duration')->default(4);
            $table->string('duration_unit', 20)->default('years');
            $table->json('study_modes')->nullable();       // ["day","evening"]
            $table->json('campuses')->nullable();          // ["Bunga","Mulago"]
            $table->decimal('tuition_amount', 15, 2)->nullable();
            $table->decimal('application_fee', 15, 2)->nullable();
            $table->string('currency', 3)->default('UGX');
            $table->string('featured_image')->nullable();
            $table->boolean('is_featured')->default(false);
            $table->enum('status', ['active', 'inactive'])->default('active')->index();
            $table->unsignedInteger('sort_order')->default(0);
            $table->timestamps();

            $table->index(['school_id', 'status']);
            $table->index(['level', 'status']);
        });

        Schema::create('programme_requirements', function (Blueprint $table) {
            $table->id();
            $table->foreignId('programme_id')->constrained()->cascadeOnDelete();
            $table->string('requirement_type', 60);        // uace_points|subject|qualification|gpa|nationality|other
            $table->string('description');
            $table->unsignedSmallInteger('minimum_points')->nullable();
            $table->string('required_subject')->nullable();
            $table->string('minimum_grade', 10)->nullable();
            $table->string('required_qualification', 60)->nullable();
            $table->decimal('minimum_gpa', 4, 2)->nullable();
            $table->boolean('is_required')->default(true);
            $table->unsignedInteger('sort_order')->default(0);
            $table->timestamps();

            $table->index(['programme_id', 'requirement_type']);
        });
    }

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