<?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('exams', function (Blueprint $table) {
            $table->id();
            $table->branchColumn();
            $table->foreignId('academic_session_id')->nullable()->constrained('academic_sessions')->nullOnDelete();
            $table->string('name');
            $table->enum('type', ['monthly', 'mid_term', 'final_term', 'custom'])->default('custom');
            $table->date('start_date')->nullable();
            $table->date('end_date')->nullable();
            $table->boolean('is_published')->default(false)->index();
            $table->auditColumns();
            $table->timestamps();
            $table->softDeletes();
        });

        // Which subjects are examined for each class, with max/passing marks.
        Schema::create('exam_papers', function (Blueprint $table) {
            $table->id();
            $table->branchColumn();
            $table->foreignId('exam_id')->constrained('exams')->cascadeOnDelete();
            $table->foreignId('class_id')->constrained('classes')->cascadeOnDelete();
            $table->foreignId('subject_id')->constrained('subjects')->cascadeOnDelete();
            $table->decimal('total_marks', 6, 2)->default(100);
            $table->decimal('passing_marks', 6, 2)->default(40);
            $table->date('exam_date')->nullable();
            $table->timestamps();

            $table->unique(['exam_id', 'class_id', 'subject_id'], 'exam_paper_unique');
        });

        Schema::create('marks', function (Blueprint $table) {
            $table->id();
            $table->branchColumn();
            $table->foreignId('exam_id')->constrained('exams')->cascadeOnDelete();
            $table->foreignId('student_id')->constrained('students')->cascadeOnDelete();
            $table->foreignId('class_id')->nullable()->constrained('classes')->nullOnDelete();
            $table->foreignId('subject_id')->constrained('subjects')->cascadeOnDelete();
            $table->decimal('obtained_marks', 6, 2)->nullable();
            $table->boolean('is_absent')->default(false);
            $table->string('remarks')->nullable();
            $table->foreignId('marked_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->unique(['exam_id', 'student_id', 'subject_id'], 'mark_unique');
            $table->index(['exam_id', 'class_id']);
        });

        Schema::create('grade_scales', function (Blueprint $table) {
            $table->id();
            $table->branchColumn();
            $table->string('grade', 5);
            $table->decimal('min_percentage', 5, 2);
            $table->decimal('max_percentage', 5, 2);
            $table->decimal('gpa', 3, 2)->default(0);
            $table->string('remark')->nullable();
            $table->timestamps();

            $table->unique(['branch_id', 'grade']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('grade_scales');
        Schema::dropIfExists('marks');
        Schema::dropIfExists('exam_papers');
        Schema::dropIfExists('exams');
    }
};
