<?php

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

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

        App\Support\SqliteCompat::statement(<<<'SQL'
CREATE TABLE `certificate_records` (
`id` int UNSIGNED NOT NULL,
  `certificate_number` varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL,
  `student_id` int UNSIGNED NOT NULL,
  `student_name` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL,
  `grade` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `cert_date` date DEFAULT NULL,
  `school_year` varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `class_section_id` int UNSIGNED DEFAULT NULL,
  `issued_by` int UNSIGNED DEFAULT NULL,
  `issued_at` datetime DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
SQL
        );

        App\Support\SqliteCompat::statement(<<<'SQL'
ALTER TABLE `certificate_records`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `certificate_number` (`certificate_number`),
  ADD KEY `idx_school_year_student` (`school_year`,`student_id`);
SQL
        );

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

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