-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "public";

-- CreateEnum
CREATE TYPE "HouseholdType" AS ENUM ('SINGLE', 'COUPLE');

-- CreateEnum
CREATE TYPE "PersonRole" AS ENUM ('PERSON_A', 'PERSON_B');

-- CreateEnum
CREATE TYPE "IncomeMode" AS ENUM ('PER_PERSON', 'HOUSEHOLD');

-- CreateEnum
CREATE TYPE "OwnerTag" AS ENUM ('PERSON_A', 'PERSON_B', 'HOUSEHOLD');

-- CreateEnum
CREATE TYPE "OneTimeEventType" AS ENUM ('INCOME', 'EXPENSE');

-- CreateEnum
CREATE TYPE "TransitionDecision" AS ENUM ('CARRY_OVER', 'SELL');

-- CreateEnum
CREATE TYPE "PositionType" AS ENUM ('SECURITY', 'REAL_ESTATE');

-- CreateTable
CREATE TABLE "AppCredential" (
    "id" TEXT NOT NULL,
    "passwordHash" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "Household" (
    "id" TEXT NOT NULL,
    "householdType" "HouseholdType" NOT NULL,
    "inflationRateDefault" DOUBLE PRECISION NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "Person" (
    "id" TEXT NOT NULL,
    "householdId" TEXT NOT NULL,
    "role" "PersonRole" NOT NULL,
    "age" INTEGER NOT NULL,
    "retirementAge" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "Plan" (
    "id" TEXT NOT NULL,
    "householdId" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "parentPlanId" TEXT,
    "branchFromPhaseId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "Phase" (
    "id" TEXT NOT NULL,
    "planId" TEXT NOT NULL,
    "sequenceNumber" INTEGER NOT NULL,
    "name" TEXT NOT NULL,
    "durationYears" INTEGER NOT NULL,
    "inflationRate" DOUBLE PRECISION,
    "incomeMode" "IncomeMode" NOT NULL DEFAULT 'HOUSEHOLD',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "IncomeEntry" (
    "id" TEXT NOT NULL,
    "phaseId" TEXT NOT NULL,
    "personId" TEXT,
    "label" TEXT,
    "amount" DOUBLE PRECISION NOT NULL,

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

-- CreateTable
CREATE TABLE "ExpenseEntry" (
    "id" TEXT NOT NULL,
    "phaseId" TEXT NOT NULL,
    "label" TEXT,
    "amount" DOUBLE PRECISION NOT NULL,

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

-- CreateTable
CREATE TABLE "Security" (
    "id" TEXT NOT NULL,
    "phaseId" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "startValue" DOUBLE PRECISION NOT NULL,
    "expectedReturn" DOUBLE PRECISION NOT NULL,
    "annualContribution" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "ownerTag" "OwnerTag" NOT NULL DEFAULT 'HOUSEHOLD',
    "saleTaxRate" DOUBLE PRECISION NOT NULL DEFAULT 0,

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

-- CreateTable
CREATE TABLE "RealEstate" (
    "id" TEXT NOT NULL,
    "phaseId" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "marketValue" DOUBLE PRECISION NOT NULL,
    "mortgage" DOUBLE PRECISION NOT NULL,
    "valueGrowth" DOUBLE PRECISION NOT NULL,
    "amortization" DOUBLE PRECISION NOT NULL,
    "salePrice" DOUBLE PRECISION,
    "saleTaxRate" DOUBLE PRECISION NOT NULL DEFAULT 20,

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

-- CreateTable
CREATE TABLE "OneTimeEvent" (
    "id" TEXT NOT NULL,
    "phaseId" TEXT NOT NULL,
    "type" "OneTimeEventType" NOT NULL,
    "amount" DOUBLE PRECISION NOT NULL,
    "description" TEXT,

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

-- CreateTable
CREATE TABLE "RetirementInfo" (
    "id" TEXT NOT NULL,
    "phaseId" TEXT NOT NULL,
    "personId" TEXT NOT NULL,
    "ahvAmount" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "pkPensionAmount" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "lumpSumAmount" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "lumpSumTaxRate" DOUBLE PRECISION NOT NULL DEFAULT 8,

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

-- CreateTable
CREATE TABLE "PhaseTransition" (
    "id" TEXT NOT NULL,
    "fromPhaseId" TEXT NOT NULL,
    "toPhaseId" TEXT NOT NULL,

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

-- CreateTable
CREATE TABLE "PhaseTransitionItem" (
    "id" TEXT NOT NULL,
    "transitionId" TEXT NOT NULL,
    "positionType" "PositionType" NOT NULL,
    "securityId" TEXT,
    "realEstateId" TEXT,
    "decision" "TransitionDecision" NOT NULL,
    "salePrice" DOUBLE PRECISION,

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

-- CreateIndex
CREATE UNIQUE INDEX "Person_householdId_role_key" ON "Person"("householdId", "role");

-- CreateIndex
CREATE UNIQUE INDEX "Phase_planId_sequenceNumber_key" ON "Phase"("planId", "sequenceNumber");

-- CreateIndex
CREATE UNIQUE INDEX "RetirementInfo_phaseId_personId_key" ON "RetirementInfo"("phaseId", "personId");

-- CreateIndex
CREATE UNIQUE INDEX "PhaseTransition_fromPhaseId_key" ON "PhaseTransition"("fromPhaseId");

-- CreateIndex
CREATE UNIQUE INDEX "PhaseTransition_toPhaseId_key" ON "PhaseTransition"("toPhaseId");

-- AddForeignKey
ALTER TABLE "Person" ADD CONSTRAINT "Person_householdId_fkey" FOREIGN KEY ("householdId") REFERENCES "Household"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Plan" ADD CONSTRAINT "Plan_householdId_fkey" FOREIGN KEY ("householdId") REFERENCES "Household"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Plan" ADD CONSTRAINT "Plan_parentPlanId_fkey" FOREIGN KEY ("parentPlanId") REFERENCES "Plan"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Phase" ADD CONSTRAINT "Phase_planId_fkey" FOREIGN KEY ("planId") REFERENCES "Plan"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IncomeEntry" ADD CONSTRAINT "IncomeEntry_phaseId_fkey" FOREIGN KEY ("phaseId") REFERENCES "Phase"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IncomeEntry" ADD CONSTRAINT "IncomeEntry_personId_fkey" FOREIGN KEY ("personId") REFERENCES "Person"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ExpenseEntry" ADD CONSTRAINT "ExpenseEntry_phaseId_fkey" FOREIGN KEY ("phaseId") REFERENCES "Phase"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Security" ADD CONSTRAINT "Security_phaseId_fkey" FOREIGN KEY ("phaseId") REFERENCES "Phase"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "RealEstate" ADD CONSTRAINT "RealEstate_phaseId_fkey" FOREIGN KEY ("phaseId") REFERENCES "Phase"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "OneTimeEvent" ADD CONSTRAINT "OneTimeEvent_phaseId_fkey" FOREIGN KEY ("phaseId") REFERENCES "Phase"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "RetirementInfo" ADD CONSTRAINT "RetirementInfo_phaseId_fkey" FOREIGN KEY ("phaseId") REFERENCES "Phase"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "RetirementInfo" ADD CONSTRAINT "RetirementInfo_personId_fkey" FOREIGN KEY ("personId") REFERENCES "Person"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "PhaseTransition" ADD CONSTRAINT "PhaseTransition_fromPhaseId_fkey" FOREIGN KEY ("fromPhaseId") REFERENCES "Phase"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "PhaseTransition" ADD CONSTRAINT "PhaseTransition_toPhaseId_fkey" FOREIGN KEY ("toPhaseId") REFERENCES "Phase"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "PhaseTransitionItem" ADD CONSTRAINT "PhaseTransitionItem_transitionId_fkey" FOREIGN KEY ("transitionId") REFERENCES "PhaseTransition"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "PhaseTransitionItem" ADD CONSTRAINT "PhaseTransitionItem_securityId_fkey" FOREIGN KEY ("securityId") REFERENCES "Security"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "PhaseTransitionItem" ADD CONSTRAINT "PhaseTransitionItem_realEstateId_fkey" FOREIGN KEY ("realEstateId") REFERENCES "RealEstate"("id") ON DELETE CASCADE ON UPDATE CASCADE;

