-- Kern-Rework: finanzielle Elemente auf Plan-Ebene (JSON pro Phase/Uebergang).
-- Fresh-Start (abgestimmt 13.07.2026): bestehende Test-Daten werden verworfen.

-- Alle bestehenden Daten entfernen (Cascade raeumt Plaene, Phasen, alte Element-Tabellen ab)
DELETE FROM "Household";

-- Alte element-spezifische Tabellen entfernen
DROP TABLE IF EXISTS "PhaseTransitionItem" CASCADE;
DROP TABLE IF EXISTS "PhaseTransition" CASCADE;
DROP TABLE IF EXISTS "IncomeEntry" CASCADE;
DROP TABLE IF EXISTS "ExpenseEntry" CASCADE;
DROP TABLE IF EXISTS "Security" CASCADE;
DROP TABLE IF EXISTS "RealEstate" CASCADE;
DROP TABLE IF EXISTS "OneTimeEvent" CASCADE;
DROP TABLE IF EXISTS "RetirementInfo" CASCADE;

-- Phase: nicht mehr benoetigte Spalten entfernen (VOR dem Drop der genutzten Enums)
ALTER TABLE "Phase" DROP COLUMN IF EXISTS "incomeMode";
ALTER TABLE "Phase" DROP COLUMN IF EXISTS "incomingCapital";

-- Alte Enums entfernen (jetzt von keiner Spalte mehr referenziert)
DROP TYPE IF EXISTS "IncomeMode";
DROP TYPE IF EXISTS "OwnerTag";
DROP TYPE IF EXISTS "OneTimeEventType";
DROP TYPE IF EXISTS "TransitionDecision";
DROP TYPE IF EXISTS "PositionType";

-- Plan: plan-spezifische Pensionsalter-Overrides
ALTER TABLE "Plan" ADD COLUMN "retirementAgeA" INTEGER;
ALTER TABLE "Plan" ADD COLUMN "retirementAgeB" INTEGER;

-- Neue Enums
CREATE TYPE "OwnerRole" AS ENUM ('PERSON_A', 'PERSON_B', 'HOUSEHOLD');
CREATE TYPE "ElementCategory" AS ENUM ('INCOME', 'EXPENSE', 'AHV', 'PENSION_FUND', 'PILLAR_3A', 'REAL_ESTATE', 'OTHER_ASSET', 'OTHER_DEBT');

-- FinancialElement
CREATE TABLE "FinancialElement" (
    "id" TEXT NOT NULL,
    "planId" TEXT NOT NULL,
    "category" "ElementCategory" NOT NULL,
    "name" TEXT NOT NULL,
    "ownerRole" "OwnerRole",
    "orderIndex" INTEGER NOT NULL DEFAULT 0,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "FinancialElement_pkey" PRIMARY KEY ("id")
);

-- ElementPhaseValue
CREATE TABLE "ElementPhaseValue" (
    "id" TEXT NOT NULL,
    "elementId" TEXT NOT NULL,
    "phaseId" TEXT NOT NULL,
    "data" JSONB NOT NULL,
    CONSTRAINT "ElementPhaseValue_pkey" PRIMARY KEY ("id")
);
CREATE UNIQUE INDEX "ElementPhaseValue_elementId_phaseId_key" ON "ElementPhaseValue"("elementId", "phaseId");

-- ElementTransitionValue
CREATE TABLE "ElementTransitionValue" (
    "id" TEXT NOT NULL,
    "elementId" TEXT NOT NULL,
    "fromPhaseId" TEXT NOT NULL,
    "data" JSONB NOT NULL,
    CONSTRAINT "ElementTransitionValue_pkey" PRIMARY KEY ("id")
);
CREATE UNIQUE INDEX "ElementTransitionValue_elementId_fromPhaseId_key" ON "ElementTransitionValue"("elementId", "fromPhaseId");

-- Foreign Keys
ALTER TABLE "FinancialElement" ADD CONSTRAINT "FinancialElement_planId_fkey" FOREIGN KEY ("planId") REFERENCES "Plan"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "ElementPhaseValue" ADD CONSTRAINT "ElementPhaseValue_elementId_fkey" FOREIGN KEY ("elementId") REFERENCES "FinancialElement"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "ElementPhaseValue" ADD CONSTRAINT "ElementPhaseValue_phaseId_fkey" FOREIGN KEY ("phaseId") REFERENCES "Phase"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "ElementTransitionValue" ADD CONSTRAINT "ElementTransitionValue_elementId_fkey" FOREIGN KEY ("elementId") REFERENCES "FinancialElement"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "ElementTransitionValue" ADD CONSTRAINT "ElementTransitionValue_fromPhaseId_fkey" FOREIGN KEY ("fromPhaseId") REFERENCES "Phase"("id") ON DELETE CASCADE ON UPDATE CASCADE;
