-- Runs AFTER the main dump (z_ sorts last).
-- Creates any missing Laravel framework tables and marks all migrations
-- as complete so `artisan migrate` is a no-op on startup.

-- ── migrations tracking ───────────────────────────────────────
CREATE TABLE IF NOT EXISTS `migrations` (
  `id`        int unsigned NOT NULL AUTO_INCREMENT,
  `migration` varchar(255) NOT NULL,
  `batch`     int NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ── users / sessions / password resets ───────────────────────
CREATE TABLE IF NOT EXISTS `users` (
  `id`                bigint unsigned NOT NULL AUTO_INCREMENT,
  `name`              varchar(255) NOT NULL,
  `email`             varchar(255) NOT NULL,
  `email_verified_at` timestamp NULL DEFAULT NULL,
  `password`          varchar(255) NOT NULL,
  `remember_token`    varchar(100) DEFAULT NULL,
  `created_at`        timestamp NULL DEFAULT NULL,
  `updated_at`        timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `users_email_unique` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `password_reset_tokens` (
  `email`      varchar(255) NOT NULL,
  `token`      varchar(255) NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `sessions` (
  `id`            varchar(255) NOT NULL,
  `user_id`       bigint unsigned DEFAULT NULL,
  `ip_address`    varchar(45) DEFAULT NULL,
  `user_agent`    text,
  `payload`       longtext NOT NULL,
  `last_activity` int NOT NULL,
  PRIMARY KEY (`id`),
  KEY `sessions_user_id_index` (`user_id`),
  KEY `sessions_last_activity_index` (`last_activity`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ── cache ─────────────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS `cache` (
  `key`        varchar(255) NOT NULL,
  `value`      mediumtext NOT NULL,
  `expiration` bigint NOT NULL,
  PRIMARY KEY (`key`),
  KEY `cache_expiration_index` (`expiration`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `cache_locks` (
  `key`        varchar(255) NOT NULL,
  `owner`      varchar(255) NOT NULL,
  `expiration` bigint NOT NULL,
  PRIMARY KEY (`key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ── queue ─────────────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS `jobs` (
  `id`           bigint unsigned NOT NULL AUTO_INCREMENT,
  `queue`        varchar(255) NOT NULL,
  `payload`      longtext NOT NULL,
  `attempts`     tinyint unsigned NOT NULL,
  `reserved_at`  int unsigned DEFAULT NULL,
  `available_at` int unsigned NOT NULL,
  `created_at`   int unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `jobs_queue_index` (`queue`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `job_batches` (
  `id`              varchar(255) NOT NULL,
  `name`            varchar(255) NOT NULL,
  `total_jobs`      int NOT NULL,
  `pending_jobs`    int NOT NULL,
  `failed_jobs`     int NOT NULL,
  `failed_job_ids`  longtext NOT NULL,
  `options`         mediumtext DEFAULT NULL,
  `cancelled_at`    int DEFAULT NULL,
  `created_at`      int NOT NULL,
  `finished_at`     int DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `failed_jobs` (
  `id`         bigint unsigned NOT NULL AUTO_INCREMENT,
  `uuid`       varchar(255) NOT NULL,
  `connection` text NOT NULL,
  `queue`      text NOT NULL,
  `payload`    longtext NOT NULL,
  `exception`  longtext NOT NULL,
  `failed_at`  timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `failed_jobs_uuid_unique` (`uuid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ── mark all migrations as complete ──────────────────────────
-- INSERT IGNORE skips rows that already exist (safe to re-run).
-- Keep this list comprehensive: any unlisted migration will be re-attempted
-- by `php artisan migrate --force` in the entrypoint. Older migrations that
-- aren't idempotent (no Schema::hasColumn guard) WILL fail when their column
-- already exists in irs990.sql. Idempotent migrations (use Schema::hasColumn /
-- hasTable / dropIfExists / IF NOT EXISTS) can be safely omitted, but listing
-- them anyway short-circuits an unnecessary ALTER attempt on boot.
INSERT IGNORE INTO `migrations` (`migration`, `batch`) VALUES
  ('0001_01_01_000000_create_users_table',                              1),
  ('0001_01_01_000001_create_cache_table',                              1),
  ('0001_01_01_000002_create_jobs_table',                               1),
  ('2026_03_28_111738_add_is_featured_to_charities_table',              2),
  ('2026_03_28_124457_add_is_latest_to_charities_table',                2),
  ('2026_03_28_125515_add_composite_index_for_charity_listing',         2),
  ('2026_03_28_131616_add_fulltext_index_to_charities_name',            2),
  ('2026_03_28_200000_recalculate_composite_scores',                    2),
  ('2026_03_29_100000_create_import_batches_table',                     3),
  ('2026_03_30_000001_add_two_factor_columns_to_users_table',           3),
  ('2026_03_30_200000_create_pages_table',                              3),
  ('2026_03_30_200001_create_navigation_items_table',                   3),
  ('2026_03_31_000001_create_column_tooltips_table',                    3),
  ('2026_04_03_000001_add_schedule_f_and_subscores_to_charities',       4),
  ('2026_04_03_000002_remove_pages_and_page_links',                     4),
  ('2026_04_03_170000_add_progress_columns_to_import_batches_table',    4),
  ('2026_04_04_000001_add_logo_path_to_charities_table',                4),
  ('2026_04_30_000001_add_visibility_and_fixed_to_column_tooltips',     5),
  ('2026_05_04_000001_add_show_stars_under_rating_to_column_tooltips',  5),
  ('2026_05_15_000001_create_site_colors_table',                        6),
  ('2026_05_15_000002_create_charity_groups_table',                     6),
  ('2026_05_15_000003_internationalize_charities_table',                6),
  ('2026_05_15_000004_add_source_to_import_batches_table',              6),
  ('2026_05_15_000005_relax_ein_nullable_on_charities',                 6),
  ('2026_05_16_000001_add_options_to_import_batches',                   7),
  ('2026_05_16_000002_add_scoring_alternative_columns',                 7);
