<?php

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

return new class extends Migration
{
    public function up(): void
    {
        App\Support\SqliteCompat::statement(<<<'SQL'
CREATE TABLE `placement_levels` (
`id` int UNSIGNED NOT NULL,
  `student_id` int UNSIGNED NOT NULL,
  `level` tinyint UNSIGNED NOT NULL,
  `school_year` varchar(20) COLLATE utf8mb4_general_ci NOT NULL,
  `created_by` int UNSIGNED DEFAULT NULL,
  `updated_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 `placement_levels`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uniq_student_year` (`student_id`,`school_year`),
  ADD KEY `idx_school_year` (`school_year`),
  ADD KEY `fk_placement_levels_created_by` (`created_by`),
  ADD KEY `fk_placement_levels_updated_by` (`updated_by`);
SQL
        );

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

        App\Support\SqliteCompat::statement(<<<'SQL'
ALTER TABLE `placement_levels`
  ADD CONSTRAINT `fk_placement_levels_created_by` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
  ADD CONSTRAINT `fk_placement_levels_student` FOREIGN KEY (`student_id`) REFERENCES `students` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `fk_placement_levels_updated_by` FOREIGN KEY (`updated_by`) REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE;
SQL
        );

    }

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