CREATE TYPE "BankTransactionDirection" AS ENUM ('DEBIT', 'CREDIT');
CREATE TYPE "BankTransactionStatus" AS ENUM ('UNMATCHED', 'MATCHED', 'RECONCILED');
CREATE TYPE "BankReconciliationStatus" AS ENUM ('DRAFT', 'SUBMITTED', 'COMPLETED');

CREATE TABLE "bank_reconciliations" (
    "id" TEXT NOT NULL,
    "accountId" TEXT NOT NULL,
    "statementStart" TIMESTAMP(3) NOT NULL,
    "statementEnd" TIMESTAMP(3) NOT NULL,
    "openingBalance" DECIMAL(14,2) NOT NULL,
    "closingBalance" DECIMAL(14,2) NOT NULL,
    "status" "BankReconciliationStatus" NOT NULL DEFAULT 'DRAFT',
    "notes" TEXT,
    "preparedBy" TEXT NOT NULL,
    "submittedAt" TIMESTAMP(3),
    "completedBy" TEXT,
    "completedAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    CONSTRAINT "bank_reconciliations_pkey" PRIMARY KEY ("id")
);

CREATE TABLE "bank_transactions" (
    "id" TEXT NOT NULL,
    "accountId" TEXT NOT NULL,
    "transactionDate" TIMESTAMP(3) NOT NULL,
    "description" TEXT NOT NULL,
    "reference" TEXT,
    "direction" "BankTransactionDirection" NOT NULL,
    "amount" DECIMAL(14,2) NOT NULL,
    "importKey" TEXT NOT NULL,
    "status" "BankTransactionStatus" NOT NULL DEFAULT 'UNMATCHED',
    "journalId" TEXT,
    "reconciliationId" TEXT,
    "createdBy" TEXT NOT NULL,
    "matchedBy" TEXT,
    "matchedAt" TIMESTAMP(3),
    "reconciledAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    CONSTRAINT "bank_transactions_pkey" PRIMARY KEY ("id")
);

CREATE UNIQUE INDEX "bank_reconciliations_accountId_statementStart_statementEnd_key" ON "bank_reconciliations"("accountId", "statementStart", "statementEnd");
CREATE INDEX "bank_reconciliations_status_statementEnd_idx" ON "bank_reconciliations"("status", "statementEnd");
CREATE UNIQUE INDEX "bank_transactions_importKey_key" ON "bank_transactions"("importKey");
CREATE UNIQUE INDEX "bank_transactions_journalId_key" ON "bank_transactions"("journalId");
CREATE INDEX "bank_transactions_accountId_transactionDate_idx" ON "bank_transactions"("accountId", "transactionDate");
CREATE INDEX "bank_transactions_status_transactionDate_idx" ON "bank_transactions"("status", "transactionDate");
CREATE INDEX "bank_transactions_reconciliationId_idx" ON "bank_transactions"("reconciliationId");

ALTER TABLE "bank_reconciliations" ADD CONSTRAINT "bank_reconciliations_accountId_fkey" FOREIGN KEY ("accountId") REFERENCES "accounts"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "bank_transactions" ADD CONSTRAINT "bank_transactions_accountId_fkey" FOREIGN KEY ("accountId") REFERENCES "accounts"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "bank_transactions" ADD CONSTRAINT "bank_transactions_journalId_fkey" FOREIGN KEY ("journalId") REFERENCES "journals"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "bank_transactions" ADD CONSTRAINT "bank_transactions_reconciliationId_fkey" FOREIGN KEY ("reconciliationId") REFERENCES "bank_reconciliations"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
