CREATE TABLE IF NOT EXISTS `events` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `code` VARCHAR(255) UNIQUE,
  `name_en` VARCHAR(255) NOT NULL,
  `name_ar` VARCHAR(255) NOT NULL,
  `date_time` DATETIME NOT NULL,
  `venue_en` VARCHAR(255) NOT NULL,
  `venue_ar` VARCHAR(255) NOT NULL,
  `maps_url` VARCHAR(2048),
  `default_language` ENUM('Arabic', 'English') NOT NULL DEFAULT 'Arabic',
  `inviter_wa_number` VARCHAR(20),
  `status` ENUM('Draft', 'Active', 'Closed') NOT NULL DEFAULT 'Draft',
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `guests` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `event_id` INT NOT NULL,
  `code` VARCHAR(255),
  `name_en` VARCHAR(255),
  `name_ar` VARCHAR(255),
  `country_code` VARCHAR(10),
  `phone` VARCHAR(20) NOT NULL,
  `language` ENUM('Arabic', 'English') NOT NULL DEFAULT 'Arabic',
  `rsvp` ENUM('No Reply', 'Attending', 'Not Attending') NOT NULL DEFAULT 'No Reply',
  `rsvp_source` VARCHAR(50),
  `rsvp_at` DATETIME NULL,
  `qr_token` VARCHAR(255) UNIQUE,
  `rsvp_token` VARCHAR(255) UNIQUE,
  `scanned` TINYINT(1) DEFAULT 0,
  `scanned_at` DATETIME NULL,
  `wati_broadcast_id` VARCHAR(64) NULL,
  `wati_message_id` VARCHAR(64) NULL,
  `wati_reply_wa_id` VARCHAR(64) NULL,
  `notes` TEXT NULL,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  FOREIGN KEY (`event_id`) REFERENCES `events`(`id`) ON DELETE CASCADE,
  UNIQUE (`event_id`, `code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `users` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `name` VARCHAR(255) NOT NULL,
  `email` VARCHAR(255) UNIQUE NOT NULL,
  `password_hash` VARCHAR(255) NOT NULL,
  `role` ENUM('admin', 'inviter', 'scanner') NOT NULL DEFAULT 'admin',
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `action_log` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `guest_id` INT NULL,
  `event_id` INT NULL,
  `user_id` INT NULL,
  `action_type` VARCHAR(100) NOT NULL,
  `old_value` TEXT,
  `new_value` TEXT,
  `source` VARCHAR(50),
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (`guest_id`) REFERENCES `guests`(`id`) ON DELETE SET NULL,
  FOREIGN KEY (`event_id`) REFERENCES `events`(`id`) ON DELETE SET NULL,
  FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `wati_reply_sessions` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `original_local_message_id` VARCHAR(64) NOT NULL,
  `reply_whatsapp_message_id` VARCHAR(64) UNIQUE NOT NULL,
  `reply_timestamp` DATETIME NOT NULL,
  `raw` TEXT,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  INDEX (`original_local_message_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
