<?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 `parent_attendance_reports` (
`id` int UNSIGNED NOT NULL,
  `parent_id` int UNSIGNED NOT NULL COMMENT 'users.id of the reporting parent (primary parent id)',
  `student_id` int UNSIGNED NOT NULL,
  `class_section_id` int UNSIGNED DEFAULT NULL,
  `report_date` date NOT NULL,
  `type` enum('absent','late','early_dismissal') NOT NULL DEFAULT 'absent',
  `arrival_time` time DEFAULT NULL,
  `dismiss_time` time DEFAULT NULL,
  `reason` text,
  `semester` varchar(32) DEFAULT NULL,
  `school_year` varchar(16) DEFAULT NULL,
  `status` enum('new','seen','processed') NOT NULL DEFAULT 'new',
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
SQL
        );

        App\Support\SqliteCompat::statement(<<<'SQL'
ALTER TABLE `parent_attendance_reports`
  ADD PRIMARY KEY (`id`),
  ADD KEY `parent_attendance_reports_parent_id_foreign` (`parent_id`),
  ADD KEY `report_date` (`report_date`),
  ADD KEY `student_id_report_date` (`student_id`,`report_date`);
SQL
        );

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

        App\Support\SqliteCompat::statement(<<<'SQL'
ALTER TABLE `parent_attendance_reports`
  ADD CONSTRAINT `parent_attendance_reports_parent_id_foreign` FOREIGN KEY (`parent_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `parent_attendance_reports_student_id_foreign` FOREIGN KEY (`student_id`) REFERENCES `students` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
SQL
        );

    }

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