-- =====================================================
-- Premier Anti-Abuse Migration: Bet Cycles Table
-- =====================================================
-- Purpose: Tracks user bet cycles to prevent single-bettor exploitation
-- Version: 2.0 (with probabilistic enforcement)
-- Created: 2025-11-18
-- =====================================================

-- Create the premier_bet_cycles table
CREATE TABLE IF NOT EXISTS `premier_bet_cycles` (
    `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    `user_id` BIGINT UNSIGNED NOT NULL,
    `consecutive_rounds` INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '0-4 (variable cycle length)',
    `cycle_phase` ENUM('loss', 'win') NOT NULL DEFAULT 'loss' COMMENT 'Current phase of the cycle',
    `last_bet_at` TIMESTAMP NULL DEFAULT NULL COMMENT 'Last bet timestamp for tracking',
    `cycle_started_at` TIMESTAMP NULL DEFAULT NULL COMMENT 'When current cycle started',
    `created_at` TIMESTAMP NULL DEFAULT NULL,
    `updated_at` TIMESTAMP NULL DEFAULT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `premier_bet_cycles_user_id_unique` (`user_id`),
    KEY `premier_bet_cycles_user_id_index` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Liga Anti-Abuse Migration: Bet Cycles Table
CREATE TABLE IF NOT EXISTS `liga_bet_cycles` (
    `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    `user_id` BIGINT UNSIGNED NOT NULL,
    `consecutive_rounds` INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '0-4 (variable cycle length)',
    `cycle_phase` ENUM('loss', 'win') NOT NULL DEFAULT 'loss' COMMENT 'Current phase of the cycle',
    `last_bet_at` TIMESTAMP NULL DEFAULT NULL COMMENT 'Last bet timestamp for tracking',
    `cycle_started_at` TIMESTAMP NULL DEFAULT NULL COMMENT 'When current cycle started',
    `created_at` TIMESTAMP NULL DEFAULT NULL,
    `updated_at` TIMESTAMP NULL DEFAULT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `liga_bet_cycles_user_id_unique` (`user_id`),
    KEY `liga_bet_cycles_user_id_index` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- =====================================================
-- Verification Queries
-- =====================================================

-- 1. Verify tables were created successfully
-- SHOW TABLES LIKE '%_bet_cycles';

-- 2. Check table structures
-- DESCRIBE premier_bet_cycles;
-- DESCRIBE liga_bet_cycles;

-- 3. Verify indexes
-- SHOW INDEX FROM premier_bet_cycles;
-- SHOW INDEX FROM liga_bet_cycles;

-- =====================================================
-- SUCCESS CRITERIA
-- =====================================================
-- ✅ Both tables appear in SHOW TABLES
-- ✅ 8 columns in each DESCRIBE output
-- ✅ 3 indexes per table (PRIMARY, UNIQUE user_id, INDEX user_id)
-- ✅ No errors in Laravel log after deployment
-- =====================================================
