-- NovaEx Database Schema
-- Optimized for one-click deployment

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- 1. Users Table
CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `uid` int(11) NOT NULL,
  `username` varchar(100) NOT NULL,
  `password` varchar(255) NOT NULL,
  `trading_password` varchar(255) DEFAULT '123456',
  `balance` decimal(30,10) DEFAULT '0.0000000000',
  `credit_score` int(11) DEFAULT '80',
  `win_loss_control` enum('none','win','loss') DEFAULT 'none',
  `status` enum('active','disabled') DEFAULT 'active',
  `kyc_name` varchar(100) DEFAULT NULL,
  `kyc_id_number` varchar(50) DEFAULT NULL,
  `kyc_id_front` varchar(255) DEFAULT NULL,
  `kyc_id_back` varchar(255) DEFAULT NULL,
  `kyc_id_handheld` varchar(255) DEFAULT NULL,
  `kyc_status` tinyint(1) DEFAULT '0',
  `last_ip` varchar(45) DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`),
  UNIQUE KEY `uid` (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 2. Admins Table
CREATE TABLE IF NOT EXISTS `admins` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(100) NOT NULL,
  `password` varchar(255) NOT NULL,
  `role` enum('super','agent') DEFAULT 'agent',
  `permissions` text,
  `remark` text,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Insert default admin (admin / 123456)
INSERT IGNORE INTO `admins` (`id`, `username`, `password`, `role`, `permissions`) VALUES
(1, 'admin', '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'super', 'all');

-- 3. Settings Table
CREATE TABLE IF NOT EXISTS `settings` (
  `name` varchar(100) NOT NULL,
  `value` text,
  PRIMARY KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Insert default settings
INSERT IGNORE INTO `settings` (`name`, `value`) VALUES
('win_rate', '70'),
('price_control', '0'),
('chat_greeting', '您好！欢迎咨询 NovaEx 官方客服。'),
('site_logo', 'assets/images/logo.png');

-- 4. Fiat Orders Table (Recharge)
CREATE TABLE IF NOT EXISTS `fiat_orders` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `amount` decimal(18,2) NOT NULL,
  `currency` varchar(10) DEFAULT 'CNY',
  `exchange_rate` decimal(18,4) DEFAULT NULL,
  `usdt_amount` decimal(18,4) DEFAULT NULL,
  `status` varchar(20) DEFAULT 'matching',
  `proof_image` varchar(255) DEFAULT NULL,
  `bank_account_info` text,
  `expires_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 5. Orders Table (Withdrawal/USDT)
CREATE TABLE IF NOT EXISTS `orders` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `type` varchar(20) DEFAULT 'usdt',
  `amount` decimal(18,8) NOT NULL,
  `currency` varchar(10) DEFAULT 'USDT',
  `account_info` text,
  `proof_img` varchar(255) DEFAULT NULL,
  `status` varchar(20) DEFAULT 'pending',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 6. Messages Table (Chat)
CREATE TABLE IF NOT EXISTS `messages` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `sender` enum('user','admin') NOT NULL,
  `message` text NOT NULL,
  `type` varchar(20) DEFAULT 'text',
  `is_read` tinyint(1) DEFAULT '0',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 7. Option Orders Table (Seconds Contract)
CREATE TABLE IF NOT EXISTS `option_orders` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `symbol` varchar(20) NOT NULL,
  `amount` decimal(18,8) NOT NULL,
  `direction` enum('up','down') NOT NULL,
  `duration` int(11) NOT NULL,
  `profit_rate` decimal(5,2) NOT NULL,
  `opening_price` decimal(18,8) NOT NULL,
  `closing_price` decimal(18,8) DEFAULT NULL,
  `status` enum('pending','completed') DEFAULT 'pending',
  `result` enum('none','win','loss') DEFAULT 'none',
  `profit` decimal(18,8) DEFAULT '0.00000000',
  `control` enum('none','win','loss') DEFAULT 'none',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `settle_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 8. User Assets Table (Spot Balances)
CREATE TABLE IF NOT EXISTS `user_assets` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `symbol` varchar(20) NOT NULL,
  `amount` decimal(30,10) DEFAULT '0.0000000000',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `user_symbol` (`user_id`,`symbol`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 9. Trading Orders Table (Spot & Futures)
CREATE TABLE IF NOT EXISTS `trading_orders` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `symbol` varchar(20) NOT NULL,
  `type` enum('spot','futures') NOT NULL,
  `side` enum('buy','sell') NOT NULL,
  `order_type` enum('limit','market') NOT NULL,
  `price` decimal(18,8) NOT NULL,
  `amount` decimal(18,8) NOT NULL,
  `total` decimal(18,8) NOT NULL,
  `leverage` int(11) DEFAULT '1',
  `tp_price` decimal(18,8) DEFAULT NULL,
  `sl_price` decimal(18,8) DEFAULT NULL,
  `status` varchar(20) DEFAULT 'open',
  `admin_status` varchar(20) DEFAULT 'pending',
  `win_loss` enum('none','win','loss') DEFAULT 'none',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

SET FOREIGN_KEY_CHECKS = 1;
