CREATE TABLE IF NOT EXISTS users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(150) UNIQUE NOT NULL,
    password VARCHAR(255) NOT NULL,
    email VARCHAR(255),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS admins (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(150) UNIQUE NOT NULL,
    password VARCHAR(255) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS accounts (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    uid VARCHAR(6) UNIQUE NOT NULL,
    account_type ENUM('SIMULATED', 'REAL') DEFAULT 'SIMULATED',
    balance DECIMAL(30, 8) DEFAULT 0,
    frozen_balance DECIMAL(30, 8) DEFAULT 0,
    credit_score INT DEFAULT 80,
    kyc_status ENUM('UNVERIFIED', 'PENDING', 'VERIFIED', 'REJECTED') DEFAULT 'UNVERIFIED',
    win_loss_control INT DEFAULT 0, -- 1: Always Win, -1: Always Loss, 0: Normal
    language VARCHAR(10) DEFAULT 'zh-hans',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS site_settings (
    id INT AUTO_INCREMENT PRIMARY KEY,
    site_name VARCHAR(100) DEFAULT 'BitCrypto',
    contact_email VARCHAR(100) DEFAULT 'support@example.com',
    deposit_address VARCHAR(255) DEFAULT 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t',
    customer_service_url TEXT,
    terms_content TEXT,
    privacy_content TEXT,
    is_pinning_active BOOLEAN DEFAULT FALSE
);

CREATE TABLE IF NOT EXISTS cryptocurrencies (
    id INT AUTO_INCREMENT PRIMARY KEY,
    symbol VARCHAR(20) UNIQUE NOT NULL,
    name VARCHAR(100) NOT NULL,
    icon_url TEXT,
    current_price DECIMAL(30, 8) DEFAULT 0,
    manual_price DECIMAL(30, 8) DEFAULT 0,
    change_24h DECIMAL(10, 2) DEFAULT 0,
    is_active BOOLEAN DEFAULT TRUE
);

CREATE TABLE IF NOT EXISTS assets (
    id INT AUTO_INCREMENT PRIMARY KEY,
    account_id INT NOT NULL,
    currency VARCHAR(10) NOT NULL,
    balance DECIMAL(30, 8) DEFAULT 0,
    frozen DECIMAL(30, 8) DEFAULT 0,
    UNIQUE KEY account_currency (account_id, currency),
    FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS orders (
    id INT AUTO_INCREMENT PRIMARY KEY,
    account_id INT NOT NULL,
    symbol VARCHAR(20) DEFAULT 'BTCUSDT',
    trade_type ENUM('SPOT', 'CONTRACT') DEFAULT 'SPOT',
    side ENUM('BUY', 'SELL') NOT NULL,
    order_type ENUM('LIMIT', 'MARKET') NOT NULL,
    price DECIMAL(30, 8),
    amount DECIMAL(30, 8) NOT NULL,
    total_usdt DECIMAL(30, 8),
    leverage INT DEFAULT 1,
    status ENUM('PENDING', 'PARTIALLY_FILLED', 'FILLED', 'CANCELED') DEFAULT 'PENDING',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS positions (
    id INT AUTO_INCREMENT PRIMARY KEY,
    account_id INT NOT NULL,
    symbol VARCHAR(20) NOT NULL,
    side ENUM('LONG', 'SHORT') NOT NULL,
    leverage INT DEFAULT 20,
    entry_price DECIMAL(30, 8) NOT NULL,
    lots DECIMAL(30, 8) NOT NULL,
    margin DECIMAL(30, 8) NOT NULL,
    is_active BOOLEAN DEFAULT TRUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS transactions (
    id INT AUTO_INCREMENT PRIMARY KEY,
    account_id INT NOT NULL,
    transaction_type ENUM('deposit', 'withdraw') NOT NULL,
    currency VARCHAR(10) DEFAULT 'USDT',
    amount DECIMAL(30, 8) DEFAULT 0,
    tx_hash VARCHAR(255),
    status ENUM('pending', 'completed', 'failed') DEFAULT 'pending',
    timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE CASCADE
);

-- Seed initial data
INSERT INTO site_settings (site_name, contact_email, deposit_address) VALUES ('BitCrypto', 'support@bitcrypto.com', 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t');
INSERT INTO admins (username, password) VALUES ('admin', '$2y$10$vK6.O/M57M.n5oYvT6pXve/tE6Yk.7Zg8XfVv0VzP2/k1e7Y6oM5e'); -- password: admin
INSERT INTO cryptocurrencies (symbol, name, icon_url, current_price, change_24h) VALUES 
('BTCUSDT', 'Bitcoin', 'https://cryptologos.cc/logos/bitcoin-btc-logo.png', 45000.00, 1.2),
('ETHUSDT', 'Ethereum', 'https://cryptologos.cc/logos/ethereum-eth-logo.png', 2500.00, -0.5),
('BNBUSDT', 'Binance Coin', 'https://cryptologos.cc/logos/binance-coin-bnb-logo.png', 300.00, 2.1),
('ADAUSDT', 'Cardano', 'https://cryptologos.cc/logos/cardano-ada-logo.png', 0.5, 3.5),
('SOLUSDT', 'Solana', 'https://cryptologos.cc/logos/solana-sol-logo.png', 100.0, 5.0),
('DOGEUSDT', 'Dogecoin', 'https://cryptologos.cc/logos/dogecoin-doge-logo.png', 0.08, -2.0);
