-- Create the users table
CREATE TABLE IF NOT EXISTS `users` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `chat` VARCHAR(255) NOT NULL UNIQUE,
    `wallet` VARCHAR(255) DEFAULT NULL,
    `balance` DECIMAL(20, 8) DEFAULT 0,
    `total_withdraw` DECIMAL(20, 8) DEFAULT 0,
    `referred` INT DEFAULT 0,
    `ref_by` VARCHAR(255) DEFAULT 'none',
    `captcha_code` VARCHAR(10) DEFAULT NULL,
    `image_captcha_verified` TINYINT DEFAULT 0,
    `conversation` VARCHAR(255) DEFAULT NULL,
    `bonus_awarded` TINYINT DEFAULT 0
);

-- Create the admins table
CREATE TABLE IF NOT EXISTS `admins` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `chat_id` VARCHAR(255) NOT NULL UNIQUE
);

-- Create the settings table
CREATE TABLE IF NOT EXISTS `settings` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `min_withdraw` DECIMAL(20, 8) DEFAULT 0,
    `max_withdraw` DECIMAL(20, 8) DEFAULT 0,
    `per_refer` DECIMAL(20, 8) DEFAULT 0,
    `bot_status` ENUM('on', 'off') DEFAULT 'off',
    `withdraw_status` ENUM('on', 'off') DEFAULT 'off'
);

-- Create the transactions table
CREATE TABLE IF NOT EXISTS `transactions` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `user_id` INT NOT NULL,
    `type` ENUM('deposit', 'withdraw', 'transfer') NOT NULL,
    `amount` DECIMAL(20, 8) NOT NULL,
    `status` ENUM('pending', 'completed', 'failed') DEFAULT 'pending',
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE
);

-- Create the logs table (for tracking admin actions)
CREATE TABLE IF NOT EXISTS `logs` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `admin_id` INT NOT NULL,
    `action` TEXT NOT NULL,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`admin_id`) REFERENCES `admins`(`id`) ON DELETE CASCADE
);