-- 1. Table for storing the unified 6-digit referral code for each user (Driver/Passenger)
CREATE TABLE `user_referral_codes` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `user_id` VARCHAR(100) NOT NULL,
  `user_type` ENUM('driver', 'passenger') NOT NULL,
  `referral_code` VARCHAR(6) NOT NULL,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_user_type_id` (`user_id`, `user_type`),
  UNIQUE KEY `idx_referral_code` (`referral_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- 2. Table for tracking the referral relationship and trip counts
CREATE TABLE `unified_referrals` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `inviter_code` VARCHAR(6) NOT NULL,
  `invited_user_id` VARCHAR(100) NOT NULL,
  `invited_user_type` ENUM('driver', 'passenger') NOT NULL,
  `status` ENUM('registered', 'active', 'completed') NOT NULL DEFAULT 'registered',
  `trip_count` INT NOT NULL DEFAULT 0,
  `is_reward_claimed` TINYINT(1) NOT NULL DEFAULT 0,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_invited_user` (`invited_user_id`, `invited_user_type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- 3. Table for Driver Cash Claims (when a driver requests to receive their reward as manual cash)
CREATE TABLE `driver_cash_claims` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `driver_id` VARCHAR(100) NOT NULL,
  `referral_id` INT NOT NULL,
  `amount_syp` INT NOT NULL,
  `status` ENUM('pending', 'paid') NOT NULL DEFAULT 'pending',
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
