<?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 `family_guardians` (
`id` int UNSIGNED NOT NULL,
  `family_id` int UNSIGNED NOT NULL,
  `user_id` int UNSIGNED NOT NULL,
  `relation` varchar(32) NOT NULL DEFAULT 'guardian',
  `is_primary` tinyint(1) NOT NULL DEFAULT '0',
  `receive_emails` tinyint(1) NOT NULL DEFAULT '1',
  `receive_sms` tinyint(1) NOT NULL DEFAULT '0',
  `custody_notes` text,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
SQL
        );

        App\Support\SqliteCompat::statement(<<<'SQL'
ALTER TABLE `family_guardians`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `family_id_user_id` (`family_id`,`user_id`),
  ADD KEY `family_guardians_user_id_foreign` (`user_id`);
SQL
        );

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

        App\Support\SqliteCompat::statement(<<<'SQL'
ALTER TABLE `family_guardians`
  ADD CONSTRAINT `family_guardians_family_id_foreign` FOREIGN KEY (`family_id`) REFERENCES `families` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `family_guardians_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
SQL
        );

    }

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