-- ==============================================================
-- SMM Bot & WebApp Complete Database Schema
-- Developer: Junayed Hossain
-- YouTube: https://www.youtube.com/@MdJunayedHassa
-- ==============================================================

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";

-- --------------------------------------------------------
-- Table structure for `settings`
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `settings` (
  `id` int(11) NOT NULL,
  `bot_token` varchar(255) DEFAULT '',
  `bot_username` varchar(255) DEFAULT '',
  `smm_api_url` varchar(255) DEFAULT 'https://smm-provider.com/api/v2',
  `smm_api_key` varchar(255) DEFAULT '',
  `force_channel` varchar(255) DEFAULT '@telegram',
  `notice` text DEFAULT NULL,
  `admin_user` varchar(100) NOT NULL DEFAULT 'admin',
  `admin_pass` varchar(255) NOT NULL,
  `refer_type` varchar(50) DEFAULT 'percentage',
  `refer_reward` decimal(10,2) DEFAULT 5.00,
  `leaderboard_by` varchar(50) DEFAULT 'spent',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Default Settings (admin / admin123)
INSERT INTO `settings` (`id`, `bot_token`, `bot_username`, `smm_api_url`, `smm_api_key`, `force_channel`, `notice`, `admin_user`, `admin_pass`, `refer_type`, `refer_reward`, `leaderboard_by`) VALUES
(1, '', 'demo_smm_bot', 'https://smm-provider.com/api/v2', '', '@telegram', '⚡ Welcome to our SMM Bot! Instant delivery & 24/7 automated service.', 'admin', '$2y$10$wT/tXn/gXGf6kF7a9MvIu.sYf4n3X4kO0bF8sY7r5xO9zT8e2f8ye', 'percentage', 5.00, 'spent')
ON DUPLICATE KEY UPDATE `id`=`id`;

-- --------------------------------------------------------
-- Table structure for `users`
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `chat_id` varchar(100) NOT NULL,
  `username` varchar(255) DEFAULT NULL,
  `first_name` varchar(255) DEFAULT NULL,
  `balance` decimal(10,2) NOT NULL DEFAULT 0.00,
  `total_spent` decimal(10,2) NOT NULL DEFAULT 0.00,
  `refer_by` varchar(100) DEFAULT NULL,
  `is_banned` tinyint(1) NOT NULL DEFAULT 0,
  `joined_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_chat_id` (`chat_id`),
  KEY `idx_refer_by` (`refer_by`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------
-- Table structure for `categories`
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `categories` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `icon` varchar(500) DEFAULT NULL,
  `status` tinyint(1) NOT NULL DEFAULT 1,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO `categories` (`id`, `name`, `icon`, `status`) VALUES
(1, 'Telegram Services', 'https://cdn-icons-png.flaticon.com/512/2111/2111646.png', 1),
(2, 'Facebook Services', 'https://cdn-icons-png.flaticon.com/512/733/733547.png', 1),
(3, 'Instagram Services', 'https://cdn-icons-png.flaticon.com/512/2111/2111463.png', 1),
(4, 'YouTube Services', 'https://cdn-icons-png.flaticon.com/512/1384/1384060.png', 1),
(5, 'TikTok Services', 'https://cdn-icons-png.flaticon.com/512/3046/3046121.png', 1);

-- --------------------------------------------------------
-- Table structure for `services`
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `services` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `category_id` int(11) NOT NULL,
  `api_service_id` varchar(100) NOT NULL,
  `name` varchar(255) NOT NULL,
  `min_qty` int(11) NOT NULL DEFAULT 1,
  `max_qty` int(11) NOT NULL DEFAULT 100000,
  `api_price` decimal(10,4) NOT NULL DEFAULT 0.0000,
  `sell_price` decimal(10,4) NOT NULL DEFAULT 0.0000,
  `status` tinyint(1) NOT NULL DEFAULT 1,
  PRIMARY KEY (`id`),
  KEY `idx_cat_id` (`category_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------
-- Table structure for `orders`
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `orders` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `api_order_id` varchar(100) DEFAULT NULL,
  `service_id` int(11) NOT NULL,
  `link` text NOT NULL,
  `quantity` int(11) NOT NULL,
  `charge` decimal(10,2) NOT NULL DEFAULT 0.00,
  `status` varchar(50) NOT NULL DEFAULT 'Pending',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------
-- Table structure for `deposits`
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `deposits` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `method` varchar(100) NOT NULL,
  `amount` decimal(10,2) NOT NULL,
  `trx_id` varchar(255) NOT NULL,
  `status` varchar(50) NOT NULL DEFAULT 'Pending',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_trx_id` (`trx_id`(191)),
  KEY `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------
-- Table structure for `gateways`
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `gateways` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `type` varchar(20) NOT NULL,
  `credentials` text DEFAULT NULL,
  `min_amount` decimal(10,2) NOT NULL DEFAULT 10.00,
  `max_amount` decimal(10,2) NOT NULL DEFAULT 10000.00,
  `status` tinyint(1) NOT NULL DEFAULT 1,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO `gateways` (`id`, `name`, `type`, `credentials`, `min_amount`, `max_amount`, `status`) VALUES
(1, 'Urontopay Auto', 'auto', '{\"base_url\":\"https:\\/\\/pay1.urontopay.com\",\"client_id\":\"demo_client_id\",\"secret_key\":\"demo_secret_key\",\"brand_key\":\"demo_brand_key\"}', 10.00, 10000.00, 1),
(2, 'bKash Personal', 'manual', '{\"instructions\":\"Send Money to 01700000000 (Personal)\\nEnter your Transaction ID below.\"}', 10.00, 5000.00, 1),
(3, 'Nagad Personal', 'manual', '{\"instructions\":\"Send Money to 01800000000 (Personal)\\nEnter your Transaction ID below.\"}', 10.00, 5000.00, 1),
(4, 'Rocket Personal', 'manual', '{\"instructions\":\"Send Money to 01900000000 (Personal)\\nEnter your Transaction ID below.\"}', 10.00, 5000.00, 1);

-- --------------------------------------------------------
-- Table structure for `milestones`
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `milestones` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL,
  `type` varchar(50) NOT NULL,
  `requirement` decimal(10,2) NOT NULL,
  `reward` decimal(10,2) NOT NULL,
  `status` tinyint(1) NOT NULL DEFAULT 1,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO `milestones` (`id`, `title`, `type`, `requirement`, `reward`, `status`) VALUES
(1, 'First Deposit Bonus', 'deposit', 100.00, 10.00, 1),
(2, 'Premium Spender Reward', 'spend', 500.00, 50.00, 1),
(3, 'Super Referrer Club', 'refer', 10.00, 25.00, 1);

-- --------------------------------------------------------
-- Table structure for `redeem_codes`
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `redeem_codes` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `code` varchar(100) NOT NULL,
  `reward` decimal(10,2) NOT NULL,
  `max_uses` int(11) NOT NULL DEFAULT 1,
  `used` int(11) NOT NULL DEFAULT 0,
  `status` tinyint(1) NOT NULL DEFAULT 1,
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_code` (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO `redeem_codes` (`id`, `code`, `reward`, `max_uses`, `used`, `status`) VALUES
(1, 'WELCOME10', 10.00, 100, 0, 1);

-- --------------------------------------------------------
-- Table structure for `user_milestones`
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `user_milestones` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `milestone_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_user_m` (`user_id`, `milestone_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------
-- Table structure for `user_redeems`
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `user_redeems` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `code_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_user_c` (`user_id`, `code_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

COMMIT;
