-- Phase 11A: ModpackChecker Licensing Tables
-- Run: psql -U arbiter -d arbiter_db -f 138_mvc_licensing.sql

BEGIN;

CREATE TABLE mvc_licenses (
    id            SERIAL PRIMARY KEY,
    order_id      VARCHAR(64) UNIQUE NOT NULL,
    buyer_id      VARCHAR(64),
    discord_id    VARCHAR(32) UNIQUE,
    tier          VARCHAR(16) NOT NULL DEFAULT 'standard',
    max_activations INTEGER NOT NULL DEFAULT 2,
    status        VARCHAR(16) NOT NULL DEFAULT 'active',
    created_at    TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE mvc_activations (
    id            SERIAL PRIMARY KEY,
    license_id    INTEGER NOT NULL REFERENCES mvc_licenses(id) ON DELETE CASCADE,
    panel_domain  VARCHAR(255) NOT NULL,
    panel_ip      VARCHAR(45),
    activated_at  TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    last_seen     TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    UNIQUE(license_id, panel_domain)
);

CREATE INDEX idx_mvc_licenses_status ON mvc_licenses(status);
CREATE INDEX idx_mvc_activations_last_seen ON mvc_activations(last_seen);

COMMIT;
