-- SQLite schema. Use docs/license_schema_mysql.sql if your VPS uses MySQL.

CREATE TABLE IF NOT EXISTS licenses (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    license_hash TEXT NOT NULL UNIQUE,
    expires_at TEXT NULL,
    features_json TEXT NOT NULL DEFAULT '{}',
    max_activations INTEGER NOT NULL DEFAULT 1,
    is_active INTEGER NOT NULL DEFAULT 1,
    customer_email TEXT NULL,
    notes TEXT NULL,
    created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS activations (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    license_id INTEGER NOT NULL,
    machine_id TEXT NOT NULL,
    app_name TEXT NOT NULL,
    app_version TEXT NOT NULL,
    activated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
    last_seen_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
    UNIQUE (license_id, machine_id),
    FOREIGN KEY (license_id) REFERENCES licenses(id) ON DELETE CASCADE
);

CREATE INDEX IF NOT EXISTS idx_activations_license_id ON activations (license_id);

-- Example license insert (replace the hash with sha256(your-real-license-key)):
-- INSERT INTO licenses (license_hash, expires_at, features_json, max_activations, customer_email)
-- VALUES ('REPLACE_WITH_SHA256_HASH', '2027-12-31T23:59:59+00:00', '{"sync":true}', 2, 'customer@example.com');
