CREATE TYPE "CutoffPhase" AS ENUM ('PRE', 'POST');

ALTER TABLE "accounting_periods"
  ADD COLUMN "cutoff_name" TEXT NOT NULL DEFAULT 'Open Air',
  ADD COLUMN "cutoff_date" TIMESTAMP(3);

ALTER TABLE "expenses"
  ADD COLUMN "cutoff_phase" "CutoffPhase" NOT NULL DEFAULT 'PRE';

CREATE TABLE "donations" (
  "id" TEXT NOT NULL,
  "title" TEXT NOT NULL,
  "description" TEXT,
  "amount" DECIMAL(10,2) NOT NULL,
  "donated_at" TIMESTAMP(3) NOT NULL,
  "period_id" TEXT NOT NULL,
  "expense_id" TEXT,
  "creator_id" TEXT NOT NULL,
  "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updated_at" TIMESTAMP(3) NOT NULL,

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

CREATE INDEX "donations_period_id_idx" ON "donations"("period_id");
CREATE INDEX "donations_expense_id_idx" ON "donations"("expense_id");
CREATE INDEX "donations_creator_id_idx" ON "donations"("creator_id");

ALTER TABLE "donations"
  ADD CONSTRAINT "donations_period_id_fkey"
  FOREIGN KEY ("period_id") REFERENCES "accounting_periods"("id")
  ON DELETE RESTRICT ON UPDATE CASCADE;

ALTER TABLE "donations"
  ADD CONSTRAINT "donations_expense_id_fkey"
  FOREIGN KEY ("expense_id") REFERENCES "expenses"("id")
  ON DELETE SET NULL ON UPDATE CASCADE;

ALTER TABLE "donations"
  ADD CONSTRAINT "donations_creator_id_fkey"
  FOREIGN KEY ("creator_id") REFERENCES "users"("id")
  ON DELETE RESTRICT ON UPDATE CASCADE;
