-- Billing, payment provider, subscription, and estate-admin onboarding support.

CREATE TYPE "EstateStatus" AS ENUM ('PENDING', 'ACTIVE', 'SUSPENDED', 'REJECTED');
CREATE TYPE "EstateSignupStatus" AS ENUM ('PENDING_APPROVAL', 'APPROVED', 'SUSPENDED', 'REJECTED');
CREATE TYPE "SubscriptionStatus" AS ENUM ('NONE', 'PLAN_SELECTED', 'PAYMENT_PENDING', 'ACTIVE', 'SUSPENDED', 'CANCELLED');
CREATE TYPE "PaymentProvider" AS ENUM ('FLUTTERWAVE', 'MONNIFY', 'PAYSTACK', 'STRIPE');
CREATE TYPE "PaymentStatus" AS ENUM ('PENDING', 'CONFIRMED', 'FAILED', 'CANCELLED');

CREATE TABLE "SubscriptionPlan" (
    "id" TEXT NOT NULL,
    "code" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "minResidents" INTEGER NOT NULL,
    "maxResidents" INTEGER,
    "monthlyPriceKobo" INTEGER,
    "currency" TEXT NOT NULL DEFAULT 'NGN',
    "customPricing" BOOLEAN NOT NULL DEFAULT false,
    "description" TEXT,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

INSERT INTO "SubscriptionPlan" (
    "id", "code", "name", "minResidents", "maxResidents", "monthlyPriceKobo", "currency", "customPricing", "description", "updatedAt"
) VALUES
    ('00000000-0000-4000-8000-000000000101', 'STARTER', 'Starter', 0, 100, 25000000, 'NGN', false, '0-100 residents/units', CURRENT_TIMESTAMP),
    ('00000000-0000-4000-8000-000000000102', 'GROWTH', 'Growth', 101, 300, 60000000, 'NGN', false, '101-300 residents/units', CURRENT_TIMESTAMP),
    ('00000000-0000-4000-8000-000000000103', 'ENTERPRISE', 'Enterprise', 301, NULL, NULL, 'NGN', true, '300+ residents/units with custom pricing', CURRENT_TIMESTAMP);

ALTER TABLE "Estate"
    ADD COLUMN "unitCount" INTEGER NOT NULL DEFAULT 0,
    ADD COLUMN "estateType" TEXT,
    ADD COLUMN "status" "EstateStatus" NOT NULL DEFAULT 'PENDING',
    ADD COLUMN "signupStatus" "EstateSignupStatus" NOT NULL DEFAULT 'PENDING_APPROVAL',
    ADD COLUMN "subscriptionStatus" "SubscriptionStatus" NOT NULL DEFAULT 'NONE',
    ADD COLUMN "subscriptionPlanId" TEXT,
    ADD COLUMN "selectedPaymentProvider" "PaymentProvider",
    ADD COLUMN "activatedAt" TIMESTAMP(3),
    ADD COLUMN "rejectedAt" TIMESTAMP(3),
    ADD COLUMN "suspendedAt" TIMESTAMP(3),
    ADD COLUMN "reviewNotes" TEXT;

UPDATE "Estate"
SET
    "status" = 'ACTIVE',
    "signupStatus" = 'APPROVED',
    "subscriptionStatus" = 'ACTIVE',
    "activatedAt" = COALESCE("activatedAt", CURRENT_TIMESTAMP)
WHERE "status" = 'PENDING';

CREATE TABLE "GlobalPaymentProviderSetting" (
    "id" TEXT NOT NULL,
    "provider" "PaymentProvider" NOT NULL,
    "displayName" TEXT NOT NULL,
    "enabled" BOOLEAN NOT NULL DEFAULT false,
    "recommendedUseCases" TEXT[] NOT NULL DEFAULT ARRAY[]::TEXT[],
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

INSERT INTO "GlobalPaymentProviderSetting" (
    "id", "provider", "displayName", "enabled", "recommendedUseCases", "metadata", "updatedAt"
) VALUES
    ('00000000-0000-4000-8000-000000000201', 'FLUTTERWAVE', 'Flutterwave', true, ARRAY['Nigeria payments', 'International/diaspora payments', 'Luxury estates'], '{}', CURRENT_TIMESTAMP),
    ('00000000-0000-4000-8000-000000000202', 'MONNIFY', 'Monnify', true, ARRAY['Nigeria payments'], '{}', CURRENT_TIMESTAMP),
    ('00000000-0000-4000-8000-000000000203', 'PAYSTACK', 'Paystack', true, ARRAY['Backup/local alternative', 'Luxury estates'], '{}', CURRENT_TIMESTAMP),
    ('00000000-0000-4000-8000-000000000204', 'STRIPE', 'Stripe', true, ARRAY['International/diaspora payments', 'Luxury estates'], '{}', CURRENT_TIMESTAMP);

CREATE TABLE "EstatePaymentProviderSetting" (
    "id" TEXT NOT NULL,
    "estateId" TEXT NOT NULL,
    "provider" "PaymentProvider" NOT NULL,
    "enabled" BOOLEAN NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

CREATE TABLE "EstatePayment" (
    "id" TEXT NOT NULL,
    "estateId" TEXT NOT NULL,
    "planId" TEXT NOT NULL,
    "provider" "PaymentProvider" NOT NULL,
    "amountKobo" INTEGER,
    "currency" TEXT NOT NULL DEFAULT 'NGN',
    "reference" TEXT,
    "status" "PaymentStatus" NOT NULL DEFAULT 'PENDING',
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "confirmedById" TEXT,
    "confirmedAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

CREATE UNIQUE INDEX "SubscriptionPlan_code_key" ON "SubscriptionPlan"("code");
CREATE INDEX "SubscriptionPlan_isActive_minResidents_idx" ON "SubscriptionPlan"("isActive", "minResidents");
CREATE INDEX "Estate_status_signupStatus_idx" ON "Estate"("status", "signupStatus");
CREATE INDEX "Estate_subscriptionPlanId_idx" ON "Estate"("subscriptionPlanId");
CREATE UNIQUE INDEX "GlobalPaymentProviderSetting_provider_key" ON "GlobalPaymentProviderSetting"("provider");
CREATE UNIQUE INDEX "EstatePaymentProviderSetting_estateId_provider_key" ON "EstatePaymentProviderSetting"("estateId", "provider");
CREATE INDEX "EstatePaymentProviderSetting_estateId_idx" ON "EstatePaymentProviderSetting"("estateId");
CREATE UNIQUE INDEX "EstatePayment_reference_key" ON "EstatePayment"("reference");
CREATE INDEX "EstatePayment_estateId_status_idx" ON "EstatePayment"("estateId", "status");
CREATE INDEX "EstatePayment_provider_status_idx" ON "EstatePayment"("provider", "status");

ALTER TABLE "Estate" ADD CONSTRAINT "Estate_subscriptionPlanId_fkey"
FOREIGN KEY ("subscriptionPlanId") REFERENCES "SubscriptionPlan"("id") ON DELETE SET NULL ON UPDATE CASCADE;

ALTER TABLE "EstatePaymentProviderSetting" ADD CONSTRAINT "EstatePaymentProviderSetting_estateId_fkey"
FOREIGN KEY ("estateId") REFERENCES "Estate"("id") ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE "EstatePayment" ADD CONSTRAINT "EstatePayment_estateId_fkey"
FOREIGN KEY ("estateId") REFERENCES "Estate"("id") ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE "EstatePayment" ADD CONSTRAINT "EstatePayment_planId_fkey"
FOREIGN KEY ("planId") REFERENCES "SubscriptionPlan"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

ALTER TABLE "EstatePayment" ADD CONSTRAINT "EstatePayment_confirmedById_fkey"
FOREIGN KEY ("confirmedById") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;
