-- ============================================================
-- GoldSlotPalace Database Setup
-- ============================================================
-- API Token: dc076dab-5076-4e76-96eb-e3587fdadaaf
-- Callback Token: fe5c7c62-9495-41d4-9834-2d19575f81fb
-- ============================================================

-- 1. Tabel konfigurasi GoldSlotPalace
CREATE TABLE IF NOT EXISTS `tb_goldslot_config` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `api_token` VARCHAR(255) NOT NULL,
    `callback_token` VARCHAR(255) NOT NULL,
    `callback_url` VARCHAR(255) DEFAULT NULL COMMENT 'URL callback untuk didaftarkan',
    `status` TINYINT(1) DEFAULT 1 COMMENT '1=active, 0=inactive',
    `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Insert default config
INSERT INTO `tb_goldslot_config` (`api_token`, `callback_token`, `callback_url`, `status`) VALUES
('dc076dab-5076-4e76-96eb-e3587fdadaaf', 'fe5c7c62-9495-41d4-9834-2d19575f81fb', 'https://yourdomain.com/function/goldslot_callback.php', 1);

-- 2. Tabel log transaksi game
CREATE TABLE IF NOT EXISTS `tb_goldslot_transactions` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `trans_id` BIGINT(20) NOT NULL COMMENT 'Transaction ID from GoldSlotPalace',
    `user_code` BIGINT(20) NOT NULL COMMENT 'User code from GoldSlotPalace',
    `round_id` VARCHAR(100) DEFAULT NULL,
    `trans_type` INT(11) DEFAULT 0 COMMENT '1=Bet, 2=Win, 4=Deposit, 8=Withdraw, 16=BetCancel, 32=BonusCall',
    `provider_id` INT(11) DEFAULT 0,
    `provider_name` VARCHAR(100) DEFAULT NULL,
    `game_code` VARCHAR(100) DEFAULT NULL,
    `game_name` VARCHAR(255) DEFAULT NULL,
    `category` VARCHAR(50) DEFAULT NULL,
    `prebalance` DECIMAL(20,2) DEFAULT 0,
    `trans_amount` DECIMAL(20,2) DEFAULT 0,
    `balance` DECIMAL(20,2) DEFAULT 0,
    `regdate` DATETIME DEFAULT NULL,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `idx_trans_id` (`trans_id`),
    KEY `idx_user_code` (`user_code`),
    KEY `idx_regdate` (`regdate`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 3. Tambah kolom goldslot_user_code ke tb_users (jika belum ada)
-- ALTER TABLE `tb_users` ADD COLUMN `goldslot_user_code` BIGINT(20) DEFAULT NULL AFTER `id`;
-- Gunakan query di bawah jika kolom belum ada:
-- Untuk cek dulu:
-- SHOW COLUMNS FROM `tb_users` LIKE 'goldslot_user_code';

-- 4. Tabel mapping provider GoldSlotPalace (untuk sinkronisasi provider_id)
CREATE TABLE IF NOT EXISTS `tb_goldslot_providers` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `provider_id` BIGINT(20) NOT NULL COMMENT 'Provider ID from API',
    `provider_name` VARCHAR(100) NOT NULL,
    `locale_name` VARCHAR(100) DEFAULT NULL,
    `status` TINYINT(1) DEFAULT 1 COMMENT '1=Normal, 2=Maintenance',
    `last_sync` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `idx_provider_id` (`provider_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;