-- Crypto Bot v2 - Database Schema
-- Run this once on your MySQL database

CREATE TABLE IF NOT EXISTS bot_config (
    id INT AUTO_INCREMENT PRIMARY KEY,
    config_key VARCHAR(64) UNIQUE NOT NULL,
    config_value TEXT,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS trades (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    token_symbol VARCHAR(32) NOT NULL,
    token_address VARCHAR(42),
    trade_type ENUM('BUY','SELL') NOT NULL,
    amount_token DECIMAL(36,18) NOT NULL,
    amount_eth DECIMAL(36,18) NOT NULL,
    price_usd DECIMAL(24,8),
    gas_cost_eth DECIMAL(24,12) DEFAULT 0,
    tx_hash VARCHAR(66) DEFAULT NULL,
    profit_loss_eth DECIMAL(36,18) DEFAULT 0,
    profit_loss_usd DECIMAL(24,8) DEFAULT 0,
    mode ENUM('PAPER','LIVE') NOT NULL DEFAULT 'PAPER',
    strategy VARCHAR(32) DEFAULT 'RSI',
    notes TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_token (token_symbol),
    INDEX idx_created (created_at),
    INDEX idx_mode (mode)
);

CREATE TABLE IF NOT EXISTS positions (
    id INT AUTO_INCREMENT PRIMARY KEY,
    token_symbol VARCHAR(32) NOT NULL,
    token_address VARCHAR(42) NOT NULL,
    amount DECIMAL(36,18) NOT NULL,
    avg_entry_price_usd DECIMAL(24,8) NOT NULL,
    entry_eth DECIMAL(36,18) NOT NULL,
    entry_rsi DECIMAL(8,4) DEFAULT NULL,
    opened_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uk_token (token_address)
);

CREATE TABLE IF NOT EXISTS bot_logs (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    level ENUM('DEBUG','INFO','WARN','ERROR') NOT NULL,
    message TEXT NOT NULL,
    context JSON DEFAULT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_created (created_at),
    INDEX idx_level (level)
);

CREATE TABLE IF NOT EXISTS price_snapshots (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    token_symbol VARCHAR(32) NOT NULL,
    price_usd DECIMAL(24,8) NOT NULL,
    rsi_14 DECIMAL(8,4) DEFAULT NULL,
    volume_24h DECIMAL(24,2) DEFAULT NULL,
    source VARCHAR(32) DEFAULT 'coingecko',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_token_time (token_symbol, created_at)
);

CREATE TABLE IF NOT EXISTS daily_stats (
    id INT AUTO_INCREMENT PRIMARY KEY,
    trade_date DATE NOT NULL UNIQUE,
    realized_pnl_eth DECIMAL(36,18) DEFAULT 0,
    realized_pnl_usd DECIMAL(24,8) DEFAULT 0,
    trades_count INT DEFAULT 0,
    gas_spent_eth DECIMAL(24,12) DEFAULT 0,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- Default config. Re-running this file will NOT overwrite values you already have.
INSERT INTO bot_config (config_key, config_value) VALUES
('paper_mode', '1'),
('max_trade_usd', '500'),
('max_daily_loss_usd', '30'),
('max_positions', '3'),
('rsi_period', '14'),
('rsi_oversold', '32'),
('rsi_overbought', '68'),
('arb_enabled', '0'),
('bot_paused', '0'),
('take_profit_pct', '3'),
('stop_loss_pct', '4'),
('min_edge_pct', '1.5'),
('pool_fee_pct', '0.30'),
('slippage_pct', '0.15'),
('max_gas_pct', '0.35'),
('cg_monthly_cap', '80000'),
('history_cache_sec', '3600')
ON DUPLICATE KEY UPDATE config_key = config_key;

-- Tokens the bot is allowed to trade (dynamic list)
CREATE TABLE IF NOT EXISTS watched_tokens (
    id INT AUTO_INCREMENT PRIMARY KEY,
    symbol VARCHAR(32) NOT NULL UNIQUE,
    coingecko_id VARCHAR(64) NOT NULL,
    contract_address VARCHAR(42) NOT NULL,
    decimals INT DEFAULT 18,
    enabled TINYINT(1) DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Seed with the current list
INSERT IGNORE INTO watched_tokens (symbol, coingecko_id, contract_address, decimals) VALUES
('WETH', 'ethereum', '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', 18),
('USDC', 'usd-coin', '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', 6),
('USDT', 'tether', '0xdAC17F958D2ee523a2206206994597C13D831ec7', 6),
('DAI', 'dai', '0x6B175474E89094C44Da98b954EedeAC495271d0F', 18),
('WBTC', 'wrapped-bitcoin', '0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599', 8),
('PEPE', 'pepe', '0x6982508145454Ce325dDbE47a25d4ec3d2311933', 18),
('LINK', 'chainlink', '0x514910771AF9Ca656af840dff83E8264EcF986CA', 18),
('UNI', 'uniswap', '0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984', 18),
('AAVE', 'aave', '0x7Fc66500c84A76Ad7e9c93437bFc5Ac33E2DDaE9', 18),
('MKR', 'maker', '0x9f8F72aA9304c8B593d555F12eF6589cC3A579A2', 18),
('CRV', 'curve-dao-token', '0xD533a949740bb3306d119CC777fa900bA034cd52', 18),
('LDO', 'lido-dao', '0x5A98FcBEA516Cf06857215779Fd812CA3beF1B32', 18),
('SHIB', 'shiba-inu', '0x95aD61b0a150d79219dCF64E1E6Cc01f0B64C4cE', 18),
('ARB', 'arbitrum', '0xB50721BCF8d664c30412Cfbc6cf7a15145234ad1', 18);
