<?php

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

return new class extends Migration
{
    public function up(): void
    {
        if (Schema::hasTable('student_decisions')) {
            return;
        }

        App\Support\SqliteCompat::statement(<<<'SQL'
CREATE TABLE `student_decisions` (
  `id` int unsigned NOT NULL,
  `student_id` int unsigned NOT NULL,
  `school_year` varchar(20) COLLATE utf8mb4_general_ci NOT NULL,
  `class_section_name` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL,
  `year_score` decimal(6,2) DEFAULT NULL,
  `decision` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL,
  `source` enum('auto','manual','pending') COLLATE utf8mb4_general_ci NOT NULL DEFAULT 'auto',
  `notes` text COLLATE utf8mb4_general_ci,
  `generated_by` int unsigned DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
SQL
        );

        App\Support\SqliteCompat::statement(<<<'SQL'
ALTER TABLE `student_decisions`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `student_id_semester_school_year` (`student_id`,`school_year`),
  ADD KEY `school_year_semester` (`school_year`);
SQL
        );

        App\Support\SqliteCompat::statement(<<<'SQL'
ALTER TABLE `student_decisions`
  MODIFY `id` int unsigned NOT NULL AUTO_INCREMENT;
SQL
        );
    }

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