CREATE DATABASE IF NOT EXISTS `gshort_license`
CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE `gshort_license`;

CREATE TABLE `licenses` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `license_key` VARCHAR(100) NOT NULL UNIQUE,
  `status` ENUM('active','inactive','blocked') NOT NULL DEFAULT 'active',
  `expires_at` DATETIME NULL,
  `activation_limit` INT UNSIGNED NOT NULL DEFAULT 1,
  `bound_domain` VARCHAR(255) NULL,
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

CREATE TABLE `activations` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `license_id` INT UNSIGNED NOT NULL,
  `domain` VARCHAR(255) NOT NULL,
  `device_id` VARCHAR(255) NULL,
  `ip_address` VARCHAR(45) NOT NULL,
  `user_agent` TEXT NULL,
  `activated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `deactivated_at` TIMESTAMP NULL,
  PRIMARY KEY (`id`),
  KEY `idx_license` (`license_id`),
  CONSTRAINT `fk_activations_license`
    FOREIGN KEY (`license_id`) REFERENCES `licenses` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB;

CREATE TABLE `api_logs` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `endpoint` VARCHAR(100) NOT NULL,
  `ip_address` VARCHAR(45) NOT NULL,
  `http_status` SMALLINT UNSIGNED NOT NULL,
  `request_data` LONGTEXT NULL,
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;
