-- /licensing-server/db_setup.sql
-- Run this on your separate licensing server database to establish the licensing tables.

CREATE TABLE IF NOT EXISTS `licenses` (
    `id` BIGINT PRIMARY KEY AUTO_INCREMENT,
    `purchase_code` VARCHAR(255) UNIQUE NOT NULL,
    `buyer_username` VARCHAR(255) NOT NULL,
    `buyer_email` VARCHAR(255) NOT NULL,
    `item_id` BIGINT DEFAULT NULL,
    `domain` VARCHAR(255) DEFAULT NULL,
    `installation_hash` VARCHAR(255) DEFAULT NULL,
    `ip_address` VARCHAR(255) DEFAULT NULL,
    `license_token` TEXT DEFAULT NULL,
    `status` ENUM('active', 'revoked', 'expired') DEFAULT 'active',
    `activation_limit` INT DEFAULT 1,
    `activated_at` TIMESTAMP NULL DEFAULT NULL,
    `last_checkin` TIMESTAMP NULL DEFAULT NULL,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `license_logs` (
    `id` BIGINT PRIMARY KEY AUTO_INCREMENT,
    `purchase_code` VARCHAR(255) NOT NULL,
    `domain` VARCHAR(255) DEFAULT NULL,
    `action` VARCHAR(255) NOT NULL,
    `ip_address` VARCHAR(255) DEFAULT NULL,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
