CREATE TABLE IF NOT EXISTS users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) NOT NULL UNIQUE,
    password_hash VARCHAR(255) NOT NULL,
    balance DECIMAL(10, 2) DEFAULT 0.00,
    role ENUM('user', 'admin') DEFAULT 'user',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS recharges (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    amount DECIMAL(10, 2) NOT NULL,
    txid VARCHAR(255),
    status ENUM('pending', 'completed', 'rejected') DEFAULT 'pending',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS sms_orders (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    request_id VARCHAR(50) NOT NULL,
    number VARCHAR(20) NOT NULL,
    service_name VARCHAR(50),
    country_name VARCHAR(50),
    cost DECIMAL(10, 2),
    sms_content TEXT,
    status ENUM('pending', 'received', 'canceled', 'expired') DEFAULT 'pending',
    expire_at TIMESTAMP NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS settings (
    setting_key VARCHAR(50) PRIMARY KEY,
    setting_value TEXT,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS support_messages (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    sender ENUM('user', 'admin') NOT NULL,
    message TEXT NOT NULL,
    is_read TINYINT(1) DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

-- Default Settings
INSERT INTO settings (setting_key, setting_value) VALUES 
('site_name', '全球接码'),
('site_logo', 'assets/pasted-20260210-082628-83f66727.png'),
('notice_text', '欢迎使用全球专业接码平台！本平台支持全球数百个国家和地区的短信验证码接收服务。'),
('usdt_trc20_address', 'TEm1B...TRC20_ADDRESS_HERE'),
('usdt_erc20_address', '0x71C...ERC20_ADDRESS_HERE'),
('lubansms_apikey', '')
ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value);

-- Default Admin (admin / admin123)
INSERT INTO users (username, password_hash, role) VALUES 
('admin', '$2y$10$QbKYSCqJI0WQLyf6NNSML.ukYrOZ0MdY61ZpK7Ekn5QQ/A9oDr.hu', 'admin')
ON DUPLICATE KEY UPDATE role = 'admin';
