-- Add new SubscriptionStatus enum values
ALTER TYPE "SubscriptionStatus" ADD VALUE IF NOT EXISTS 'PAYMENT_PENDING';
ALTER TYPE "SubscriptionStatus" ADD VALUE IF NOT EXISTS 'SUSPENDED';
ALTER TYPE "SubscriptionStatus" ADD VALUE IF NOT EXISTS 'EXPIRED';
ALTER TYPE "SubscriptionStatus" ADD VALUE IF NOT EXISTS 'PAUSED';

-- Add new InvoiceStatus enum value
ALTER TYPE "InvoiceStatus" ADD VALUE IF NOT EXISTS 'VOIDED';

-- Extend subscriptions table (camelCase to match Prisma default)
ALTER TABLE "subscriptions"
  ADD COLUMN IF NOT EXISTS "trialUsed"           BOOLEAN   NOT NULL DEFAULT false,
  ADD COLUMN IF NOT EXISTS "paymentPendingSince"  TIMESTAMP(3),
  ADD COLUMN IF NOT EXISTS "paymentDueAt"         TIMESTAMP(3),
  ADD COLUMN IF NOT EXISTS "pastDueSince"         TIMESTAMP(3),
  ADD COLUMN IF NOT EXISTS "suspendedAt"          TIMESTAMP(3),
  ADD COLUMN IF NOT EXISTS "endedAt"              TIMESTAMP(3),
  ADD COLUMN IF NOT EXISTS "retryCount"           INTEGER   NOT NULL DEFAULT 0,
  ADD COLUMN IF NOT EXISTS "maxRetryCount"        INTEGER   NOT NULL DEFAULT 5;

-- Extend subscription_invoices table
ALTER TABLE "subscription_invoices"
  ADD COLUMN IF NOT EXISTS "providerInvoiceId" TEXT,
  ADD COLUMN IF NOT EXISTS "dueAt"             TIMESTAMP(3),
  ADD COLUMN IF NOT EXISTS "failedAt"          TIMESTAMP(3),
  ADD COLUMN IF NOT EXISTS "voidedAt"          TIMESTAMP(3);

-- Create subscription_events table
CREATE TABLE IF NOT EXISTS "subscription_events" (
  "id"             TEXT         NOT NULL,
  "subscriptionId" TEXT         NOT NULL,
  "companyId"      TEXT         NOT NULL,
  "eventType"      TEXT         NOT NULL,
  "source"         TEXT         NOT NULL DEFAULT 'system',
  "payload"        JSONB        NOT NULL DEFAULT '{}',
  "occurredAt"     TIMESTAMP(3) NOT NULL,
  "createdAt"      TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT "subscription_events_pkey" PRIMARY KEY ("id")
);

CREATE INDEX IF NOT EXISTS "subscription_events_subscriptionId_idx" ON "subscription_events"("subscriptionId");
CREATE INDEX IF NOT EXISTS "subscription_events_companyId_idx" ON "subscription_events"("companyId");

ALTER TABLE "subscription_events"
  ADD CONSTRAINT "subscription_events_subscriptionId_fkey"
  FOREIGN KEY ("subscriptionId") REFERENCES "subscriptions"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- Create payment_attempts table
CREATE TABLE IF NOT EXISTS "payment_attempts" (
  "id"                TEXT         NOT NULL,
  "invoiceId"         TEXT         NOT NULL,
  "subscriptionId"    TEXT         NOT NULL,
  "providerPaymentId" TEXT,
  "status"            TEXT         NOT NULL,
  "failureCode"       TEXT,
  "failureMessage"    TEXT,
  "attemptedAt"       TIMESTAMP(3) NOT NULL,
  "createdAt"         TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT "payment_attempts_pkey" PRIMARY KEY ("id")
);

CREATE INDEX IF NOT EXISTS "payment_attempts_invoiceId_idx" ON "payment_attempts"("invoiceId");

ALTER TABLE "payment_attempts"
  ADD CONSTRAINT "payment_attempts_invoiceId_fkey"
  FOREIGN KEY ("invoiceId") REFERENCES "subscription_invoices"("id") ON DELETE CASCADE ON UPDATE CASCADE;
