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 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,
    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',
    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),
    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 'BTC-USDT',
    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) VALUES ('BitCrypto');
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);
