-- Phase E/F: WHMCS reconciliation, events, usage exports

CREATE TYPE "IntegrationEventStatus" AS ENUM ('PENDING', 'ACKNOWLEDGED', 'DEAD_LETTER');
CREATE TYPE "ReconciliationSeverity" AS ENUM ('INFO', 'WARNING', 'BLOCKING', 'DESTRUCTIVE', 'SECURITY');
CREATE TYPE "UsageExportStatus" AS ENUM ('DRAFT', 'EXPORTED', 'ADJUSTED');

ALTER TABLE "whmcs_service_links"
  ADD COLUMN "lastReconciledAt" TIMESTAMPTZ(3);

CREATE TABLE "integration_events" (
  "id" TEXT NOT NULL,
  "installationId" TEXT NOT NULL,
  "eventType" TEXT NOT NULL,
  "payload" JSONB NOT NULL,
  "dedupeKey" TEXT NOT NULL,
  "status" "IntegrationEventStatus" NOT NULL DEFAULT 'PENDING',
  "acknowledgedAt" TIMESTAMPTZ(3),
  "deadLetteredAt" TIMESTAMPTZ(3),
  "deadLetterReason" TEXT,
  "createdAt" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT "integration_events_pkey" PRIMARY KEY ("id")
);

CREATE UNIQUE INDEX "integration_events_dedupeKey_key" ON "integration_events"("dedupeKey");
CREATE INDEX "integration_events_installationId_status_createdAt_idx" ON "integration_events"("installationId", "status", "createdAt");
CREATE INDEX "integration_events_installationId_createdAt_idx" ON "integration_events"("installationId", "createdAt");

ALTER TABLE "integration_events"
  ADD CONSTRAINT "integration_events_installationId_fkey"
  FOREIGN KEY ("installationId") REFERENCES "whmcs_installations"("id") ON DELETE CASCADE ON UPDATE CASCADE;

CREATE TABLE "integration_sync_cursors" (
  "id" TEXT NOT NULL,
  "installationId" TEXT NOT NULL,
  "eventsCursor" TEXT,
  "updatedAt" TIMESTAMPTZ(3) NOT NULL,
  CONSTRAINT "integration_sync_cursors_pkey" PRIMARY KEY ("id")
);

CREATE UNIQUE INDEX "integration_sync_cursors_installationId_key" ON "integration_sync_cursors"("installationId");

ALTER TABLE "integration_sync_cursors"
  ADD CONSTRAINT "integration_sync_cursors_installationId_fkey"
  FOREIGN KEY ("installationId") REFERENCES "whmcs_installations"("id") ON DELETE CASCADE ON UPDATE CASCADE;

CREATE TABLE "reconciliation_issues" (
  "id" TEXT NOT NULL,
  "installationId" TEXT NOT NULL,
  "runId" TEXT NOT NULL,
  "externalServiceId" TEXT,
  "serverId" TEXT,
  "code" TEXT NOT NULL,
  "severity" "ReconciliationSeverity" NOT NULL,
  "message" TEXT NOT NULL,
  "details" JSONB,
  "autoRepaired" BOOLEAN NOT NULL DEFAULT false,
  "resolvedAt" TIMESTAMPTZ(3),
  "createdAt" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT "reconciliation_issues_pkey" PRIMARY KEY ("id")
);

CREATE INDEX "reconciliation_issues_installationId_runId_idx" ON "reconciliation_issues"("installationId", "runId");
CREATE INDEX "reconciliation_issues_installationId_resolvedAt_idx" ON "reconciliation_issues"("installationId", "resolvedAt");

ALTER TABLE "reconciliation_issues"
  ADD CONSTRAINT "reconciliation_issues_installationId_fkey"
  FOREIGN KEY ("installationId") REFERENCES "whmcs_installations"("id") ON DELETE CASCADE ON UPDATE CASCADE;

CREATE TABLE "whmcs_usage_exports" (
  "id" TEXT NOT NULL,
  "installationId" TEXT NOT NULL,
  "externalServiceId" TEXT NOT NULL,
  "serverId" TEXT NOT NULL,
  "periodStart" TIMESTAMPTZ(3) NOT NULL,
  "periodEnd" TIMESTAMPTZ(3) NOT NULL,
  "metrics" JSONB NOT NULL,
  "contentHash" TEXT NOT NULL,
  "status" "UsageExportStatus" NOT NULL DEFAULT 'DRAFT',
  "isTestMode" BOOLEAN NOT NULL DEFAULT false,
  "adjustmentOfId" TEXT,
  "exportedAt" TIMESTAMPTZ(3),
  "createdAt" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT "whmcs_usage_exports_pkey" PRIMARY KEY ("id")
);

CREATE UNIQUE INDEX "whmcs_usage_exports_installationId_externalServiceId_periodStart_periodEnd_isTestMode_key"
  ON "whmcs_usage_exports"("installationId", "externalServiceId", "periodStart", "periodEnd", "isTestMode");
CREATE INDEX "whmcs_usage_exports_installationId_periodEnd_idx" ON "whmcs_usage_exports"("installationId", "periodEnd");

ALTER TABLE "whmcs_usage_exports"
  ADD CONSTRAINT "whmcs_usage_exports_installationId_fkey"
  FOREIGN KEY ("installationId") REFERENCES "whmcs_installations"("id") ON DELETE CASCADE ON UPDATE CASCADE;
