-- CreateEnum
CREATE TYPE "LicenseStatus" AS ENUM ('active', 'suspended', 'expired', 'revoked');

-- CreateTable
CREATE TABLE "licenses" (
    "id" UUID NOT NULL,
    "key_hash" TEXT NOT NULL,
    "key_last4" VARCHAR(4) NOT NULL,
    "plan_tier" "PlanTier" NOT NULL,
    "status" "LicenseStatus" NOT NULL DEFAULT 'active',
    "sites_allowed" INTEGER NOT NULL,
    "expires_at" TIMESTAMP(3) NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "licenses_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "licenses_key_hash_key" ON "licenses"("key_hash");

-- CreateIndex
CREATE INDEX "licenses_status_expires_at_idx" ON "licenses"("status", "expires_at");

-- CreateIndex
CREATE INDEX "licenses_plan_tier_idx" ON "licenses"("plan_tier");

-- CreateTable
CREATE TABLE "license_activations" (
    "id" UUID NOT NULL,
    "license_id" UUID NOT NULL,
    "site_url" TEXT NOT NULL,
    "activated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "deactivated_at" TIMESTAMP(3),

    CONSTRAINT "license_activations_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "license_activations_license_id_site_url_key" ON "license_activations"("license_id", "site_url");

-- CreateIndex
CREATE INDEX "license_activations_license_id_idx" ON "license_activations"("license_id");

-- CreateIndex
CREATE INDEX "license_activations_site_url_idx" ON "license_activations"("site_url");

-- AddForeignKey
ALTER TABLE "license_activations" ADD CONSTRAINT "license_activations_license_id_fkey"
  FOREIGN KEY ("license_id") REFERENCES "licenses"("id") ON DELETE CASCADE ON UPDATE CASCADE;
