<?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 `semester_scores` (
`id` int UNSIGNED NOT NULL,
  `student_id` int NOT NULL,
  `school_id` char(36) COLLATE utf8mb4_unicode_ci NOT NULL,
  `class_section_id` int DEFAULT NULL,
  `updated_by` int DEFAULT NULL,
  `homework_avg` float DEFAULT NULL,
  `quiz_avg` float DEFAULT NULL,
  `project_avg` float DEFAULT NULL,
  `midterm_exam_score` float DEFAULT NULL,
  `final_exam_score` float DEFAULT NULL,
  `attendance_score` float DEFAULT NULL,
  `participation_score` float DEFAULT NULL,
  `ptap_score` decimal(5,2) DEFAULT NULL,
  `test_avg` float DEFAULT NULL,
  `semester_score` float DEFAULT NULL,
  `semester` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
  `school_year` varchar(9) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
  `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
SQL
        );

        App\Support\SqliteCompat::statement(<<<'SQL'
ALTER TABLE `semester_scores`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uq_semester_scores_nk` (`student_id`,`class_section_id`,`semester`,`school_year`),
  ADD KEY `idx_student_id` (`student_id`),
  ADD KEY `idx_semester_school_year` (`semester`,`school_year`);
SQL
        );

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

    }

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