<?php

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

// Evaluation schema ported from education-erp: monthly continuous assessment per
// class, with per-student/per-subject marks and per-student notes.
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('evaluations', function (Blueprint $table) {
            $table->id();
            $table->foreignId('branch_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('class_id')->constrained('school_classes')->cascadeOnDelete();
            $table->integer('month_shamsi');
            $table->string('month_name_fa')->nullable();
            $table->integer('year_shamsi')->default(1403);
            $table->string('evaluator_name')->nullable();
            $table->string('title_fa')->nullable();
            $table->string('title_en')->nullable();
            $table->text('evaluator_note')->nullable();
            $table->string('status')->default('draft'); // draft|in_progress|completed|published
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('evaluation_results', function (Blueprint $table) {
            $table->id();
            $table->foreignId('evaluation_id')->constrained()->cascadeOnDelete();
            $table->foreignId('student_id')->constrained()->cascadeOnDelete();
            $table->foreignId('subject_id')->constrained()->cascadeOnDelete();
            $table->decimal('marks', 6, 2)->nullable();
            $table->integer('max_marks')->default(100);
            $table->text('note')->nullable();
            $table->timestamps();
            $table->unique(['evaluation_id', 'student_id', 'subject_id'], 'eval_result_unique');
        });

        Schema::create('evaluation_student_notes', function (Blueprint $table) {
            $table->id();
            $table->foreignId('evaluation_id')->constrained()->cascadeOnDelete();
            $table->foreignId('student_id')->constrained()->cascadeOnDelete();
            $table->text('note')->nullable();
            $table->boolean('is_critical')->default(false);
            $table->timestamps();
            $table->unique(['evaluation_id', 'student_id'], 'eval_note_unique');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('evaluation_student_notes');
        Schema::dropIfExists('evaluation_results');
        Schema::dropIfExists('evaluations');
    }
};
