-- ============================================================
-- Adbell Blog Network — License Server Schema
-- license.adbellmedia.com
-- ============================================================

CREATE TABLE IF NOT EXISTS `abn_licenses` (
  `id`             INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `license_key`    VARCHAR(32) NOT NULL UNIQUE,
  `tier`           ENUM('single','agency','developer') NOT NULL,
  `email`          VARCHAR(255) NOT NULL,
  `name`           VARCHAR(255) NOT NULL DEFAULT '',
  `order_ref`      VARCHAR(100) NOT NULL DEFAULT '',
  `domains`        TEXT DEFAULT NULL,
  `max_domains`    TINYINT UNSIGNED NOT NULL DEFAULT 1,
  `status`         ENUM('active','suspended','refunded') NOT NULL DEFAULT 'active',
  `issued_at`      DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `expires_at`     DATETIME DEFAULT NULL,
  `last_check`     DATETIME DEFAULT NULL,
  `check_count`    INT UNSIGNED NOT NULL DEFAULT 0,
  `notes`          TEXT DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `abn_activations` (
  `id`             INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `license_id`     INT UNSIGNED NOT NULL,
  `domain`         VARCHAR(255) NOT NULL,
  `ip_address`     VARCHAR(45) NOT NULL DEFAULT '',
  `php_version`    VARCHAR(20) DEFAULT NULL,
  `script_version` VARCHAR(20) DEFAULT NULL,
  `activated_at`   DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `last_ping`      DATETIME DEFAULT NULL,
  `ping_count`     INT UNSIGNED NOT NULL DEFAULT 0,
  `status`         ENUM('active','deactivated') NOT NULL DEFAULT 'active',
  FOREIGN KEY (`license_id`) REFERENCES `abn_licenses`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `abn_api_log` (
  `id`             INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `license_key`    VARCHAR(32) NOT NULL,
  `action`         VARCHAR(50) NOT NULL,
  `domain`         VARCHAR(255) DEFAULT NULL,
  `result`         VARCHAR(50) NOT NULL,
  `ip_address`     VARCHAR(45) NOT NULL,
  `created_at`     DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  INDEX `idx_key` (`license_key`),
  INDEX `idx_created` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
