-- VIP PANEL — Database Schema
-- Run once in phpMyAdmin on fresh install

CREATE TABLE IF NOT EXISTS `bm_admins` (
  `id`             INT AUTO_INCREMENT PRIMARY KEY,
  `username`       VARCHAR(80)   NOT NULL UNIQUE,
  `password`       VARCHAR(255)  NOT NULL,
  `token`          VARCHAR(64)   NOT NULL UNIQUE,
  `balance`        DECIMAL(12,2) NOT NULL DEFAULT 0,
  `reseller_limit` INT           NOT NULL DEFAULT 5,
  `reseller_count` INT           NOT NULL DEFAULT 0,
  `max_devices`    INT           NOT NULL DEFAULT 1,
  `status`         ENUM('active','blocked') NOT NULL DEFAULT 'active',
  `created_by`     VARCHAR(80)   NOT NULL DEFAULT 'owner',
  `created_at`     DATETIME      NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `bm_admin_devices` (
  `id`         INT AUTO_INCREMENT PRIMARY KEY,
  `admin_id`   INT         NOT NULL,
  `device_id`  VARCHAR(64) NOT NULL,
  `created_at` DATETIME    NOT NULL DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY `uq_admin_dev` (`admin_id`,`device_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `bm_resellers` (
  `id`           INT AUTO_INCREMENT PRIMARY KEY,
  `username`     VARCHAR(80)   NOT NULL UNIQUE,
  `password`     VARCHAR(255)  NOT NULL,
  `token`        VARCHAR(64)   NOT NULL UNIQUE,
  `unique_id`    VARCHAR(32)   NOT NULL UNIQUE,
  `balance`      DECIMAL(12,2) NOT NULL DEFAULT 0,
  `max_devices`  INT           NOT NULL DEFAULT 1,
  `status`       ENUM('active','blocked') NOT NULL DEFAULT 'active',
  `created_by`   VARCHAR(80)   NOT NULL DEFAULT 'owner',
  `rs_server_on` TINYINT(1)    NOT NULL DEFAULT 1,
  `rs_maint_msg` VARCHAR(1000)          DEFAULT NULL,
  `rs_tg_link`   VARCHAR(500)           DEFAULT NULL,
  `created_at`   DATETIME      NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `bm_reseller_devices` (
  `id`          INT AUTO_INCREMENT PRIMARY KEY,
  `reseller_id` INT         NOT NULL,
  `device_id`   VARCHAR(64) NOT NULL,
  `created_at`  DATETIME    NOT NULL DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY `uq_rs_dev` (`reseller_id`,`device_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `bm_keys` (
  `id`              INT AUTO_INCREMENT PRIMARY KEY,
  `key_value`       VARCHAR(120) NOT NULL UNIQUE,
  `duration`        VARCHAR(10)  NOT NULL,
  `max_devices`     INT          NOT NULL DEFAULT 1,
  `device_count`    INT          NOT NULL DEFAULT 0,
  `status`          ENUM('active','blocked','expired') NOT NULL DEFAULT 'active',
  `created_by`      VARCHAR(80)  NOT NULL,
  `created_by_role` ENUM('owner','admin','reseller') NOT NULL DEFAULT 'owner',
  `expires_at`      DATETIME     NOT NULL,
  `created_at`      DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `bm_key_devices` (
  `id`         INT AUTO_INCREMENT PRIMARY KEY,
  `key_id`     INT         NOT NULL,
  `device_id`  VARCHAR(64) NOT NULL,
  `created_at` DATETIME    NOT NULL DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY `uq_key_dev` (`key_id`,`device_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `bm_server` (
  `id`        INT AUTO_INCREMENT PRIMARY KEY,
  `is_on`     TINYINT(1)    NOT NULL DEFAULT 1,
  `maint_msg` VARCHAR(1000)          DEFAULT 'SERVER IS TEMPORARY OFF, KINDLY WAIT.',
  `tg_link`   VARCHAR(500)           DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT IGNORE INTO `bm_server` (`id`,`is_on`,`maint_msg`,`tg_link`)
VALUES (1, 1, 'SERVER IS TEMPORARY OFF, KINDLY WAIT.', '');
