/*
  Warnings:

  - You are about to drop the `ChurchChild` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `ChurchDocument` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `ChurchMember` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `ContentBlock` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `Donation` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `FellowshipGroup` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `Ministry` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `MinistryInterest` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `TeamMember` table. If the table is not empty, all the data it contains will be lost.

*/
-- CreateEnum
CREATE TYPE "CashupStatus" AS ENUM ('open', 'closed');

-- CreateEnum
CREATE TYPE "CashupAction" AS ENUM ('closed', 'quick_closed', 'reopened');

-- CreateEnum
CREATE TYPE "EventCostType" AS ENUM ('once_off', 'per_item');

-- DropForeignKey
ALTER TABLE "ChurchChild" DROP CONSTRAINT "ChurchChild_memberId_fkey";

-- DropForeignKey
ALTER TABLE "ChurchMember" DROP CONSTRAINT "ChurchMember_fellowshipGroupId_fkey";

-- DropForeignKey
ALTER TABLE "Donation" DROP CONSTRAINT "Donation_linkedMemberId_fkey";

-- DropForeignKey
ALTER TABLE "Ministry" DROP CONSTRAINT "Ministry_leaderId_fkey";

-- DropForeignKey
ALTER TABLE "MinistryInterest" DROP CONSTRAINT "MinistryInterest_memberId_fkey";

-- DropForeignKey
ALTER TABLE "MinistryInterest" DROP CONSTRAINT "MinistryInterest_ministryId_fkey";

-- AlterTable
ALTER TABLE "Event" ADD COLUMN     "cashupDraft" JSONB,
ADD COLUMN     "cashupStatus" "CashupStatus" NOT NULL DEFAULT 'open',
ADD COLUMN     "closedAt" TIMESTAMP(3),
ADD COLUMN     "closedById" TEXT,
ADD COLUMN     "reopenedAt" TIMESTAMP(3),
ADD COLUMN     "reopenedById" TEXT;

-- DropTable
DROP TABLE "ChurchChild";

-- DropTable
DROP TABLE "ChurchDocument";

-- DropTable
DROP TABLE "ChurchMember";

-- DropTable
DROP TABLE "ContentBlock";

-- DropTable
DROP TABLE "Donation";

-- DropTable
DROP TABLE "FellowshipGroup";

-- DropTable
DROP TABLE "Ministry";

-- DropTable
DROP TABLE "MinistryInterest";

-- DropTable
DROP TABLE "TeamMember";

-- CreateTable
CREATE TABLE "EventCost" (
    "id" TEXT NOT NULL,
    "eventId" TEXT NOT NULL,
    "eventOptionId" TEXT,
    "label" TEXT NOT NULL,
    "costType" "EventCostType" NOT NULL,
    "amount" DOUBLE PRECISION NOT NULL,
    "notes" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "EventCashup" (
    "id" TEXT NOT NULL,
    "eventId" TEXT NOT NULL,
    "action" "CashupAction" NOT NULL,
    "donationsWrittenOff" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "totalCosts" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "totalExpectedRevenue" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "totalActualRevenue" DOUBLE PRECISION,
    "notes" TEXT,
    "performedById" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "EventCashupLine" (
    "id" TEXT NOT NULL,
    "cashupId" TEXT NOT NULL,
    "method" TEXT NOT NULL,
    "expectedAmount" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "actualAmount" DOUBLE PRECISION,
    "variance" DOUBLE PRECISION,
    "notes" TEXT,

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

-- CreateIndex
CREATE INDEX "EventCost_eventId_idx" ON "EventCost"("eventId");

-- CreateIndex
CREATE INDEX "EventCost_eventOptionId_idx" ON "EventCost"("eventOptionId");

-- CreateIndex
CREATE INDEX "EventCashup_eventId_createdAt_idx" ON "EventCashup"("eventId", "createdAt");

-- CreateIndex
CREATE INDEX "EventCashupLine_cashupId_idx" ON "EventCashupLine"("cashupId");

-- CreateIndex
CREATE INDEX "Event_closedById_idx" ON "Event"("closedById");

-- CreateIndex
CREATE INDEX "Event_reopenedById_idx" ON "Event"("reopenedById");

-- AddForeignKey
ALTER TABLE "Event" ADD CONSTRAINT "Event_closedById_fkey" FOREIGN KEY ("closedById") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Event" ADD CONSTRAINT "Event_reopenedById_fkey" FOREIGN KEY ("reopenedById") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "EventCost" ADD CONSTRAINT "EventCost_eventId_fkey" FOREIGN KEY ("eventId") REFERENCES "Event"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "EventCost" ADD CONSTRAINT "EventCost_eventOptionId_fkey" FOREIGN KEY ("eventOptionId") REFERENCES "EventOption"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "EventCashup" ADD CONSTRAINT "EventCashup_eventId_fkey" FOREIGN KEY ("eventId") REFERENCES "Event"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "EventCashup" ADD CONSTRAINT "EventCashup_performedById_fkey" FOREIGN KEY ("performedById") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "EventCashupLine" ADD CONSTRAINT "EventCashupLine_cashupId_fkey" FOREIGN KEY ("cashupId") REFERENCES "EventCashup"("id") ON DELETE CASCADE ON UPDATE CASCADE;
