-- Fix bet_id_sequence table to support larger bet IDs
-- This resolves the "Numeric value out of range" error

-- Step 1: Alter the current_id column to BIGINT UNSIGNED
ALTER TABLE `bet_id_sequence` 
MODIFY COLUMN `current_id` BIGINT UNSIGNED NOT NULL DEFAULT 0;

-- Step 2: Check current value and reset if it's unreasonably large
-- If current_id is greater than 1 trillion (too large), reset it to a reasonable value
UPDATE `bet_id_sequence` 
SET `current_id` = GREATEST(
    1000000000,  -- Minimum 10-digit bet ID (1 billion)
    (SELECT COALESCE(MAX(id), 0) FROM `premier_bets`),
    (SELECT COALESCE(MAX(id), 0) FROM `liga_bets`),
    (SELECT COALESCE(MAX(id), 0) FROM `champions_bets`),
    (SELECT COALESCE(MAX(id), 0) FROM `keno_bets`),
    (SELECT COALESCE(MAX(id), 0) FROM `bets`)
) 
WHERE `id` = 1 AND `current_id` > 1000000000000;  -- Only reset if > 1 trillion

-- Step 3: Verify the fix
SELECT 
    `id`,
    `current_id`,
    CASE 
        WHEN `current_id` < 1000000000 THEN 'Too small (< 1 billion)'
        WHEN `current_id` > 1000000000000 THEN 'Too large (> 1 trillion)'
        ELSE 'Valid range'
    END as status
FROM `bet_id_sequence`
WHERE `id` = 1;
