<?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 `whatsapp_group_memberships` (
`id` int UNSIGNED NOT NULL,
  `class_section_id` int NOT NULL,
  `school_year` varchar(25) NOT NULL,
  `semester` varchar(20) NOT NULL,
  `subject_type` enum('primary','second') NOT NULL DEFAULT 'primary',
  `subject_id` int NOT NULL COMMENT 'users.id for primary; parents.id for second',
  `is_member` tinyint(1) NOT NULL DEFAULT '0',
  `verified_by` int DEFAULT NULL,
  `verified_at` datetime DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

-- --------------------------------------------------------

--
-- Table structure for table `whatsapp_invites_log`
--


CREATE TABLE `whatsapp_invites_log` (

`id` int NOT NULL,

  `parent_id` int NOT NULL,

  `email` varchar(255) COLLATE utf8mb4_general_ci NOT NULL,

  `class_section_id` int DEFAULT NULL,

  `link_id` int DEFAULT NULL,

  `status` varchar(20) COLLATE utf8mb4_general_ci NOT NULL,

  `error_message` text COLLATE utf8mb4_general_ci,

  `sent_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
SQL
        );

        App\Support\SqliteCompat::statement(<<<'SQL'
ALTER TABLE `whatsapp_group_memberships`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uniq_whatsapp_membership` (`class_section_id`,`school_year`,`semester`,`subject_type`,`subject_id`),
  ADD KEY `class_section_id_school_year_semester` (`class_section_id`,`school_year`,`semester`);
SQL
        );

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

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