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

CREATE TABLE IF NOT EXISTS licenses (
    id INT AUTO_INCREMENT PRIMARY KEY,
    license_key VARCHAR(36) NOT NULL UNIQUE,
    product_id VARCHAR(50) NOT NULL DEFAULT 'default',
    customer_email VARCHAR(255),
    customer_name VARCHAR(255),
    max_activations INT NOT NULL DEFAULT 1,
    active_activations INT NOT NULL DEFAULT 0,
    status ENUM('active', 'suspended', 'expired') NOT NULL DEFAULT 'active',
    expires_at DATETIME NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    notes TEXT
);

CREATE TABLE IF NOT EXISTS activations (
    id INT AUTO_INCREMENT PRIMARY KEY,
    license_key VARCHAR(36) NOT NULL,
    machine_id VARCHAR(100) NOT NULL,
    machine_name VARCHAR(255),
    platform VARCHAR(100),
    app_version VARCHAR(50),
    ip_address VARCHAR(45),
    activated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    last_verified_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    UNIQUE KEY unique_license_machine (license_key, machine_id),
    FOREIGN KEY (license_key) REFERENCES licenses(license_key) ON DELETE CASCADE
);

-- Insert some sample license keys for testing
INSERT INTO licenses (license_key, product_id, customer_email, customer_name, max_activations, status) VALUES
    (UUID(), 'my_app', 'test@example.com', 'Test User', 2, 'active'),
    (UUID(), 'my_app', 'demo@example.com', 'Demo User', 1, 'active');
