<?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 `attendance_day` (
`id` int UNSIGNED NOT NULL,
  `class_section_id` int NOT NULL,
  `date` date NOT NULL,
  `status` enum('draft','submitted','published') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'draft',
  `submitted_by` int DEFAULT NULL,
  `submitted_at` datetime DEFAULT NULL,
  `published_by` int DEFAULT NULL,
  `published_at` datetime DEFAULT NULL,
  `auto_publish_at` datetime DEFAULT NULL,
  `reopened_by` int DEFAULT NULL,
  `reopened_at` datetime DEFAULT NULL,
  `reopen_reason` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `semester` varchar(25) COLLATE utf8mb4_unicode_ci NOT NULL,
  `school_year` varchar(9) COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
SQL
        );

        App\Support\SqliteCompat::statement(<<<'SQL'
ALTER TABLE `attendance_day`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uniq_section_date_term` (`class_section_id`,`date`,`semester`,`school_year`),
  ADD KEY `idx_status_autopublish` (`status`,`auto_publish_at`),
  ADD KEY `idx_term` (`semester`,`school_year`);
SQL
        );

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

    }

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