CREATE TYPE "AccountType" AS ENUM ('ASSET', 'LIABILITY', 'EQUITY', 'REVENUE', 'EXPENSE');
CREATE TYPE "NormalBalance" AS ENUM ('DEBIT', 'CREDIT');
CREATE TYPE "AccountingPeriodStatus" AS ENUM ('OPEN', 'CLOSED');
CREATE TYPE "JournalStatus" AS ENUM ('POSTED', 'REVERSED', 'REVERSAL');

ALTER TABLE "expense_requests" ADD COLUMN "journalId" TEXT;

CREATE TABLE "accounts" (
  "id" TEXT NOT NULL, "code" TEXT NOT NULL, "name" TEXT NOT NULL,
  "type" "AccountType" NOT NULL, "normalBalance" "NormalBalance" NOT NULL,
  "isClientControl" BOOLEAN NOT NULL DEFAULT false, "isBankAccount" BOOLEAN NOT NULL DEFAULT false,
  "isActive" BOOLEAN NOT NULL DEFAULT true, "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL, CONSTRAINT "accounts_pkey" PRIMARY KEY ("id")
);

CREATE TABLE "accounting_periods" (
  "id" TEXT NOT NULL, "name" TEXT NOT NULL, "startsAt" TIMESTAMP(3) NOT NULL, "endsAt" TIMESTAMP(3) NOT NULL,
  "status" "AccountingPeriodStatus" NOT NULL DEFAULT 'OPEN', "closedAt" TIMESTAMP(3), "closedBy" TEXT,
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "updatedAt" TIMESTAMP(3) NOT NULL,
  CONSTRAINT "accounting_periods_pkey" PRIMARY KEY ("id")
);

CREATE TABLE "journals" (
  "id" TEXT NOT NULL, "reference" TEXT NOT NULL, "periodId" TEXT NOT NULL, "date" TIMESTAMP(3) NOT NULL,
  "description" TEXT NOT NULL, "status" "JournalStatus" NOT NULL DEFAULT 'POSTED', "sourceType" TEXT NOT NULL,
  "sourceId" TEXT, "caseId" TEXT, "clientId" TEXT, "postedBy" TEXT NOT NULL,
  "postedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "reversalOfId" TEXT,
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "journals_pkey" PRIMARY KEY ("id")
);

CREATE TABLE "journal_lines" (
  "id" TEXT NOT NULL, "journalId" TEXT NOT NULL, "accountId" TEXT NOT NULL, "description" TEXT,
  "debit" DECIMAL(14,2) NOT NULL DEFAULT 0, "credit" DECIMAL(14,2) NOT NULL DEFAULT 0,
  "caseId" TEXT, "clientId" TEXT, "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT "journal_lines_pkey" PRIMARY KEY ("id")
);

CREATE UNIQUE INDEX "accounts_code_key" ON "accounts"("code");
CREATE INDEX "accounts_type_isActive_idx" ON "accounts"("type", "isActive");
CREATE UNIQUE INDEX "accounting_periods_name_key" ON "accounting_periods"("name");
CREATE INDEX "accounting_periods_status_startsAt_endsAt_idx" ON "accounting_periods"("status", "startsAt", "endsAt");
CREATE UNIQUE INDEX "journals_reference_key" ON "journals"("reference");
CREATE UNIQUE INDEX "journals_reversalOfId_key" ON "journals"("reversalOfId");
CREATE INDEX "journals_periodId_date_idx" ON "journals"("periodId", "date");
CREATE INDEX "journals_caseId_date_idx" ON "journals"("caseId", "date");
CREATE INDEX "journals_clientId_date_idx" ON "journals"("clientId", "date");
CREATE INDEX "journals_sourceType_sourceId_idx" ON "journals"("sourceType", "sourceId");
CREATE INDEX "journal_lines_journalId_idx" ON "journal_lines"("journalId");
CREATE INDEX "journal_lines_accountId_idx" ON "journal_lines"("accountId");
CREATE INDEX "journal_lines_caseId_idx" ON "journal_lines"("caseId");
CREATE INDEX "journal_lines_clientId_idx" ON "journal_lines"("clientId");
CREATE UNIQUE INDEX "expense_requests_journalId_key" ON "expense_requests"("journalId");

ALTER TABLE "journals" ADD CONSTRAINT "journals_periodId_fkey" FOREIGN KEY ("periodId") REFERENCES "accounting_periods"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "journals" ADD CONSTRAINT "journals_reversalOfId_fkey" FOREIGN KEY ("reversalOfId") REFERENCES "journals"("id") ON DELETE SET NULL ON UPDATE CASCADE;
ALTER TABLE "journal_lines" ADD CONSTRAINT "journal_lines_journalId_fkey" FOREIGN KEY ("journalId") REFERENCES "journals"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "journal_lines" ADD CONSTRAINT "journal_lines_accountId_fkey" FOREIGN KEY ("accountId") REFERENCES "accounts"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "expense_requests" ADD CONSTRAINT "expense_requests_journalId_fkey" FOREIGN KEY ("journalId") REFERENCES "journals"("id") ON DELETE SET NULL ON UPDATE CASCADE;
