-- ============================================================
-- FireWin777 -- MIGRATION 2026-11-18 (FireWin777 parity).
-- For EXISTING FireWin databases only. Import ONCE in phpMyAdmin
-- (select the FireWin database > Import > choose this file).
-- SAFE TO RE-RUN: every statement is guarded -- CREATE TABLE
-- IF NOT EXISTS, or an INFORMATION_SCHEMA check that adds /
-- widens a column only when it is missing / too narrow, or an
-- INSERT ... ON DUPLICATE KEY UPDATE that never overwrites an
-- existing setting value. If you re-run it, the guarded
-- statements simply do nothing (you will see "1" rows / 0
-- affected rows). No table is dropped, renamed or emptied, and
-- no existing row is changed except:
--   * payment_methods bonus % seeds (only where bonus_percent
--     is still 0 after the column is added -- see the note
--     above those UPDATEs; skip them if the admin already set
--     different % values in Admin -> Payments).
-- BACK UP the database first (phpMyAdmin > Export), as always.
-- No passwords, secrets or credentials are in this file.
-- ============================================================
SET NAMES utf8mb4;
SET time_zone = '+00:00';

-- ------------------------------------------------------------
-- 1) payment_methods.bonus_percent (FireWin per-method deposit
--    bonus % -- orders.php line 7, helpers.php
--    ws_maybe_method_bonus()). Missing entirely in FireWin v2.
-- ------------------------------------------------------------
SET @col := (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'payment_methods' AND COLUMN_NAME = 'bonus_percent');
SET @sql := IF(@col = 0, 'ALTER TABLE payment_methods ADD COLUMN bonus_percent DECIMAL(6,2) NOT NULL DEFAULT 0 AFTER instructions', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- ------------------------------------------------------------
-- 2) payment_methods.icon: widen VARCHAR(40) -> VARCHAR(600).
--    FireWin manage.php records that VARCHAR(40) cut any image
--    URL / uploaded photo to 40 chars and broke the method image.
--    Guarded: runs only while the column is still narrower than
--    600 chars. Widening never loses data. NOTE: FireWin's
--    manage.php comment says its patch widens icon to MEDIUMTEXT,
--    but no such patch file exists in the FireWin set -- the
--    widest real value an icon holds is an image URL / path, and
--    every other URL column in this schema is VARCHAR(600), so
--    VARCHAR(600) is used here. If the backend fix ever stores
--    base64 image data in icon instead of a URL, widen it to
--    MEDIUMTEXT at that point (same guarded pattern).
-- ------------------------------------------------------------
SET @col := (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'payment_methods' AND COLUMN_NAME = 'icon' AND CHARACTER_MAXIMUM_LENGTH < 600);
SET @sql := IF(@col = 1, 'ALTER TABLE payment_methods MODIFY COLUMN icon VARCHAR(600) DEFAULT NULL', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- ------------------------------------------------------------
-- 3) orders.method_bonus_percent -- snapshot of the method %
--    shown at checkout (FireWin orders.php line 34). A later
--    admin change to the method % must not change old orders.
-- ------------------------------------------------------------
SET @col := (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'orders' AND COLUMN_NAME = 'method_bonus_percent');
SET @sql := IF(@col = 0, 'ALTER TABLE orders ADD COLUMN method_bonus_percent DECIMAL(6,2) DEFAULT NULL AFTER method_name', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- ------------------------------------------------------------
-- 4) Payment-method bonus % seed values (FireWin live values:
--    Cash App 10%, Chime 15%, Debit Card 0%). These UPDATEs
--    touch ONLY rows whose bonus_percent is still 0 -- i.e.
--    rows that never had a % set. If the admin already set a %
--    in Admin -> Payments, that value is kept. If you do NOT
--    want these defaults, delete this block before importing.
-- ------------------------------------------------------------
UPDATE payment_methods SET bonus_percent = 10 WHERE name = 'Cash App' AND bonus_percent = 0;
UPDATE payment_methods SET bonus_percent = 15 WHERE name = 'Chime' AND bonus_percent = 0;

-- ------------------------------------------------------------
-- 5) Missing settings keys (FireWin backend reads them with
--    ws_setting() and falls back to these same defaults when
--    the row is missing, so a missing row is not fatal -- but
--    seeding them makes the Admin panel show the real values).
--    ON DUPLICATE KEY UPDATE setting_value = setting_value
--    means: an existing value is NEVER overwritten.
--    Transfer rule (2026-11-20): Bonus -> Main is FREE, minimum
--    $0.10, player gets 100% (transfer_min '0.1', transfer_fee
--    '0'). The wallet code enforces the rule itself; these two
--    rows only feed the balance display. An existing DB keeps
--    any value the admin already set.
-- ------------------------------------------------------------
INSERT INTO site_settings (setting_key, setting_value) VALUES
  ('signup_bonus_enabled', '1'),
  ('signup_bonus_percent', '150'),
  ('withdraw_mult', '4'),
  ('max_accounts_per_device', '5'),
  ('checkin_json', '{"days":[50,75,100,150,200,300,500]}'),
  ('bonuses_json', '[]'),
  ('instant_games_json', '[]'),
  ('transfer_min', '0.1'),
  ('transfer_fee', '0'),
  ('maintenance_expires_at', NULL),
  ('maintenance_status', 'active')
ON DUPLICATE KEY UPDATE setting_value = setting_value;

-- Bring rows still on the OLD shipped transfer defaults up to the
-- new free-transfer rule. Only the exact old defaults are touched
-- ('1'/'20' min, '0.10' fee): any value the admin set by hand is
-- left alone.
UPDATE site_settings SET setting_value = '0.1' WHERE setting_key = 'transfer_min' AND setting_value IN ('1', '20');
UPDATE site_settings SET setting_value = '0' WHERE setting_key = 'transfer_fee' AND setting_value = '0.10';

-- ------------------------------------------------------------
-- 5b) Buyer maintenance-fee submissions table + settings note.
--    Same definition as install.sql. maintenance_expires_at
--    NULL = active, no expiry set yet; GMPanel sets it on the
--    first approval (site_sync.php action=maintenance_set).
--    No Binance ID / WhatsApp / prices are seeded here: those
--    live in GMPanel and are pulled into maintenance_config_json
--    by api/maintenance.php?action=refresh_config.
-- ------------------------------------------------------------
CREATE TABLE IF NOT EXISTS maintenance_submissions (
  id INT AUTO_INCREMENT PRIMARY KEY,
  months TINYINT NOT NULL,
  amount DECIMAL(10,2) NOT NULL,
  binance_id VARCHAR(128) NOT NULL DEFAULT '',
  proof_path VARCHAR(255) NOT NULL DEFAULT '',
  status ENUM('pending','approved','rejected') NOT NULL DEFAULT 'pending',
  submitted_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  decided_at DATETIME NULL DEFAULT NULL,
  decided_by VARCHAR(64) NULL DEFAULT NULL,
  panel_submission_id INT NULL DEFAULT NULL,
  INDEX idx_maintenance_status (status),
  INDEX idx_maintenance_panel (panel_submission_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ------------------------------------------------------------
-- 6) Instant-game score + history tables + direct-API store
--    tables (same definitions as install-new.sql).
-- ------------------------------------------------------------

-- ============================================================
-- FIREWIN UPDATE 2026-11-18 -- FireWin777 parity additions.
-- 22 new tables (13 instant-game score/history + 9 direct-API
-- store tables). Nothing here renames, drops or alters any of
-- the existing FireWin tables. All new tables use InnoDB +
-- utf8mb4, matching the rest of this file.
-- ============================================================

-- ------------------------------------------------------------
-- INSTANT GAMES (6 games x score row + history log)
-- Shape copied from FireWin: aviator (firewin777-aviator-score.sql),
-- plinko (plinko.sql), roulette (roulette.sql). Slot 777, Cashbull
-- and Joker 777 have NO .sql file in the FireWin set -- their
-- CREATE was never shipped as a file -- so they mirror the
-- aviator shape exactly (same columns the game APIs use:
-- slot777.php / cashbull.php / joker777.php lines 15-24).
-- Every *_score_tx.type is VARCHAR(20) from the start (NOT the
-- old ENUM('add','redeem','bet','win')): the 2026-11-04 FireWin
-- bug (patch-instant-score-type.sql) was that admin_add /
-- admin_remove and the 'forfeit' row on a full-6X redeem could
-- not save in strict MySQL and rolled back. VARCHAR(20) holds
-- add, redeem, bet, win, forfeit, admin_add, admin_remove.
-- wallet is 'main' or 'bonus' (Score split Main/Bonus).
-- The 4X/6X redeem rule reads the LATEST 'add' row per wallet
-- from these tx tables (see instant_admin.php / *_score_tx
-- queries), so do not prune old tx rows.
-- ------------------------------------------------------------

-- Slot 777
CREATE TABLE IF NOT EXISTS slot777_scores (
  user_id INT UNSIGNED NOT NULL PRIMARY KEY,
  main_score DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  bonus_score DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  CONSTRAINT fk_slot777_scores_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS slot777_score_tx (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  user_id INT UNSIGNED NOT NULL,
  wallet ENUM('main','bonus') NOT NULL,
  type VARCHAR(20) NOT NULL,
  amount DECIMAL(12,2) NOT NULL,
  main_score_after DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  bonus_score_after DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_slot777_tx_user (user_id),
  INDEX idx_slot777_tx_created (created_at),
  CONSTRAINT fk_slot777_tx_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Cashbull
CREATE TABLE IF NOT EXISTS cashbull_scores (
  user_id INT UNSIGNED NOT NULL PRIMARY KEY,
  main_score DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  bonus_score DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  CONSTRAINT fk_cashbull_scores_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS cashbull_score_tx (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  user_id INT UNSIGNED NOT NULL,
  wallet ENUM('main','bonus') NOT NULL,
  type VARCHAR(20) NOT NULL,
  amount DECIMAL(12,2) NOT NULL,
  main_score_after DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  bonus_score_after DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_cashbull_tx_user (user_id),
  INDEX idx_cashbull_tx_created (created_at),
  CONSTRAINT fk_cashbull_tx_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Joker 777
CREATE TABLE IF NOT EXISTS joker777_scores (
  user_id INT UNSIGNED NOT NULL PRIMARY KEY,
  main_score DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  bonus_score DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  CONSTRAINT fk_joker777_scores_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS joker777_score_tx (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  user_id INT UNSIGNED NOT NULL,
  wallet ENUM('main','bonus') NOT NULL,
  type VARCHAR(20) NOT NULL,
  amount DECIMAL(12,2) NOT NULL,
  main_score_after DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  bonus_score_after DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_joker777_tx_user (user_id),
  INDEX idx_joker777_tx_created (created_at),
  CONSTRAINT fk_joker777_tx_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Aviator (copied from FireWin firewin777-aviator-score.sql,
-- except score_tx.type is VARCHAR(20) -- see note above --
-- instead of the original ENUM('add','redeem','bet','win')).
CREATE TABLE IF NOT EXISTS aviator_scores (
  user_id INT UNSIGNED NOT NULL PRIMARY KEY,
  main_score DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  bonus_score DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  CONSTRAINT fk_aviator_scores_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS aviator_score_tx (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  user_id INT UNSIGNED NOT NULL,
  wallet ENUM('main','bonus') NOT NULL,
  type VARCHAR(20) NOT NULL,
  amount DECIMAL(12,2) NOT NULL,
  main_score_after DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  bonus_score_after DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_aviator_tx_user (user_id),
  INDEX idx_aviator_tx_created (created_at),
  CONSTRAINT fk_aviator_tx_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS aviator_rounds (
  id INT AUTO_INCREMENT PRIMARY KEY,
  user_id INT NOT NULL,
  wallet VARCHAR(10) NOT NULL DEFAULT 'main',
  bet DECIMAL(10,2) NOT NULL,
  crash_point DECIMAL(10,2) NOT NULL,
  crash_duration DECIMAL(10,3) NOT NULL,
  status VARCHAR(20) NOT NULL DEFAULT 'pending',
  start_time DATETIME NULL,
  cashout_multiplier DECIMAL(10,2) NULL,
  win DECIMAL(10,2) NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_aviator_rounds_user (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Plinko (copied from FireWin plinko.sql; ENGINE/charset added,
-- the FireWin file omits them -- columns unchanged).
CREATE TABLE IF NOT EXISTS plinko_scores (
  user_id INT NOT NULL PRIMARY KEY,
  main_score DECIMAL(12,2) NOT NULL DEFAULT 0,
  bonus_score DECIMAL(12,2) NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS plinko_score_tx (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  user_id INT NOT NULL,
  wallet VARCHAR(10) NOT NULL,
  type VARCHAR(20) NOT NULL,
  amount DECIMAL(12,2) NOT NULL,
  main_score_after DECIMAL(12,2) NOT NULL DEFAULT 0,
  bonus_score_after DECIMAL(12,2) NOT NULL DEFAULT 0,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_plinko_tx_user (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Roulette (copied from FireWin roulette.sql; ENGINE/charset
-- added, the FireWin file omits them -- columns unchanged).
CREATE TABLE IF NOT EXISTS roulette_scores (
  user_id INT NOT NULL PRIMARY KEY,
  main_score DECIMAL(12,2) NOT NULL DEFAULT 0,
  bonus_score DECIMAL(12,2) NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS roulette_score_tx (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  user_id INT NOT NULL,
  wallet VARCHAR(10) NOT NULL,
  type VARCHAR(20) NOT NULL,
  amount DECIMAL(12,2) NOT NULL,
  main_score_after DECIMAL(12,2) NOT NULL DEFAULT 0,
  bonus_score_after DECIMAL(12,2) NOT NULL DEFAULT 0,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_roulette_tx_user (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ------------------------------------------------------------
-- DIRECT-API STORE TABLES (Yolo 777 / Fire Kirin / Game Vault)
-- Copied from FireWin yolo.sql, firekirin.sql, gamevault.sql.
-- These are the tables the direct store APIs use (api/yolo.php,
-- api/firekirin.php, api/gamevault.php on FireWin): one player
-- link row per customer, a throttle row so the store rate limit
-- is respected, and an append-only event log shown in the admin
-- store panel. Only needed if FireWin adopts the direct-API
-- game panels; harmless if unused. (FireWin already has
-- auto_jobs + store_connections from the older worker system;
-- these 9 tables are separate and do not replace them.)
-- ------------------------------------------------------------

-- Yolo 777
CREATE TABLE IF NOT EXISTS yolo_players (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  user_id INT UNSIGNED NOT NULL,
  yolo_user_id BIGINT UNSIGNED NOT NULL,
  account VARCHAR(120) NOT NULL DEFAULT '',
  balance DECIMAL(12,2) NOT NULL DEFAULT 0,
  last_sync_at DATETIME DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY uq_yp_user (user_id),
  UNIQUE KEY uq_yp_yolo (yolo_user_id),
  INDEX idx_yp_account (account),
  CONSTRAINT fk_yp_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS yolo_throttle (
  yolo_user_id BIGINT UNSIGNED NOT NULL PRIMARY KEY,
  last_call_ms BIGINT UNSIGNED NOT NULL,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS yolo_log (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  yolo_user_id BIGINT UNSIGNED DEFAULT NULL,
  event VARCHAR(40) NOT NULL,
  detail TEXT DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_yl_user (yolo_user_id),
  INDEX idx_yl_created (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Fire Kirin
CREATE TABLE IF NOT EXISTS fk_players (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  user_id INT UNSIGNED NOT NULL,
  account VARCHAR(64) NOT NULL DEFAULT '',
  balance DECIMAL(12,2) NOT NULL DEFAULT 0,
  last_sync_at DATETIME DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY uq_fk_user (user_id),
  UNIQUE KEY uq_fk_account (account),
  CONSTRAINT fk_fk_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS fk_throttle (
  scope VARCHAR(20) NOT NULL PRIMARY KEY,
  last_call_ms BIGINT UNSIGNED NOT NULL,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS fk_log (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  account VARCHAR(64) DEFAULT NULL,
  event VARCHAR(40) NOT NULL,
  detail TEXT DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_fk_account (account),
  INDEX idx_fk_created (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Game Vault
CREATE TABLE IF NOT EXISTS gv_players (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  user_id INT UNSIGNED NOT NULL,
  account VARCHAR(64) NOT NULL DEFAULT '',
  gv_user_id VARCHAR(64) NOT NULL DEFAULT '',
  balance VARCHAR(64) NOT NULL DEFAULT '0',
  last_sync_at DATETIME DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY uq_gv_user (user_id),
  UNIQUE KEY uq_gv_account (account),
  UNIQUE KEY uq_gv_gvuser (gv_user_id),
  CONSTRAINT fk_gv_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS gv_throttle (
  scope VARCHAR(20) NOT NULL PRIMARY KEY,
  last_call_ms BIGINT UNSIGNED NOT NULL,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS gv_log (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  account VARCHAR(64) DEFAULT NULL,
  event VARCHAR(40) NOT NULL,
  detail TEXT DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_gv_account (account),
  INDEX idx_gv_created (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ------------------------------------------------------------
-- 7) Safety net: if any of the four score_tx tables already
--    existed with the old ENUM('add','redeem','bet','win') type
--    (e.g. a partial earlier import), widen type to VARCHAR(20)
--    so admin_add / admin_remove / forfeit rows save. This is
--    the FireWin patch-instant-score-type.sql fix (2026-11-04).
--    On a normal run these tables were just created above with
--    VARCHAR(20) already, so these four MODIFYs change nothing.
-- ------------------------------------------------------------
ALTER TABLE slot777_score_tx MODIFY COLUMN type VARCHAR(20) NOT NULL;
ALTER TABLE cashbull_score_tx MODIFY COLUMN type VARCHAR(20) NOT NULL;
ALTER TABLE joker777_score_tx MODIFY COLUMN type VARCHAR(20) NOT NULL;
ALTER TABLE aviator_score_tx MODIFY COLUMN type VARCHAR(20) NOT NULL;
