-- ============================================================
-- License Manager Database Schema
-- Import this into a NEW, separate database (do NOT mix with
-- your SMMHUT panel database). e.g. `license_manager`
-- ============================================================

CREATE TABLE IF NOT EXISTS `licenses` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `client_name` VARCHAR(150) NOT NULL,
  `domain` VARCHAR(255) NOT NULL,
  `license_key` VARCHAR(64) NOT NULL,
  `plan` VARCHAR(50) DEFAULT 'Standard',
  `status` ENUM('active','disabled') NOT NULL DEFAULT 'active',
  `api_balance` DECIMAL(12,2) DEFAULT 0.00,
  `last_admin_login` DATETIME NULL,
  `last_seen` DATETIME NULL,
  `ip_address` VARCHAR(45) NULL,
  `script_version` VARCHAR(20) NULL,
  `notes` TEXT NULL,
  `expires_at` DATE NULL,
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY `uniq_domain` (`domain`),
  UNIQUE KEY `uniq_license_key` (`license_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `admin_users` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `username` VARCHAR(50) NOT NULL UNIQUE,
  `password_hash` VARCHAR(255) NOT NULL,
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `activity_log` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `license_id` INT NULL,
  `action` VARCHAR(100) NOT NULL,
  `details` TEXT NULL,
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (`license_id`) REFERENCES `licenses`(`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Default admin login: username = admin / password = ChangeMe123!
-- (Hash generated with PHP password_hash — change immediately after first login)
INSERT INTO `admin_users` (`username`, `password_hash`) VALUES
('admin', '$2y$10$P7ynOIxTB1tI89vFaKf.1OcWMfmy5TkUi5VsfzIlFdxAPfGaUMocG');

-- Example client row (delete after testing)
INSERT INTO `licenses` (`client_name`, `domain`, `license_key`, `plan`, `status`, `api_balance`)
VALUES ('Demo Client', 'demo-client.com', 'DEMO-KEY-1234567890', 'Standard', 'active', 25.00);
