-- CreateEnum
CREATE TYPE "ProceedingType" AS ENUM ('PRIMARY_SUIT', 'INTERLOCUTORY_APPLICATION', 'APPEAL', 'EXECUTION', 'REVIEW', 'OTHER');

-- CreateEnum
CREATE TYPE "ProceedingStatus" AS ENUM ('PREPARING', 'FILED', 'ACTIVE', 'STAYED', 'JUDGMENT_DELIVERED', 'EXECUTION', 'APPEALED', 'CLOSED');

-- CreateEnum
CREATE TYPE "FilingStatus" AS ENUM ('PREPARED', 'SUBMITTED', 'ACCEPTED', 'REJECTED', 'SERVED', 'WITHDRAWN');

-- CreateEnum
CREATE TYPE "CourtEventType" AS ENUM ('MENTION', 'DIRECTIONS', 'HEARING', 'RULING', 'JUDGMENT', 'MEDIATION', 'TAXATION', 'EXECUTION', 'OTHER');

-- CreateEnum
CREATE TYPE "CourtEventStatus" AS ENUM ('SCHEDULED', 'ADJOURNED', 'COMPLETED', 'CANCELLED');

-- CreateEnum
CREATE TYPE "ServiceStatus" AS ENUM ('PENDING', 'ATTEMPTED', 'SERVED', 'FAILED', 'DISPENSED_WITH');

-- CreateEnum
CREATE TYPE "CourtOrderType" AS ENUM ('DIRECTIONS', 'INTERLOCUTORY_RULING', 'FINAL_JUDGMENT', 'CONSENT', 'COSTS', 'STAY', 'EXECUTION_ORDER', 'APPEAL_ORDER', 'OTHER');

-- CreateEnum
CREATE TYPE "OrderStatus" AS ENUM ('ACTIVE', 'COMPLIED', 'STAYED', 'SET_ASIDE', 'APPEALED');

-- CreateTable
CREATE TABLE "court_proceedings" (
    "id" TEXT NOT NULL,
    "caseId" TEXT NOT NULL,
    "courtName" TEXT NOT NULL,
    "station" TEXT,
    "registry" TEXT,
    "courtCaseNumber" TEXT NOT NULL,
    "proceedingType" "ProceedingType" NOT NULL,
    "status" "ProceedingStatus" NOT NULL DEFAULT 'PREPARING',
    "filedAt" TIMESTAMP(3),
    "presidingOfficer" TEXT,
    "opposingAdvocate" TEXT,
    "notes" TEXT,
    "createdBy" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "court_filings" (
    "id" TEXT NOT NULL,
    "proceedingId" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "documentType" TEXT NOT NULL,
    "version" INTEGER NOT NULL DEFAULT 1,
    "status" "FilingStatus" NOT NULL DEFAULT 'PREPARED',
    "filedAt" TIMESTAMP(3),
    "filingReference" TEXT,
    "receiptReference" TEXT,
    "rejectionReason" TEXT,
    "serviceRequired" BOOLEAN NOT NULL DEFAULT false,
    "responseDueAt" TIMESTAMP(3),
    "notes" TEXT,
    "createdBy" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "court_events" (
    "id" TEXT NOT NULL,
    "proceedingId" TEXT NOT NULL,
    "type" "CourtEventType" NOT NULL,
    "title" TEXT NOT NULL,
    "scheduledAt" TIMESTAMP(3) NOT NULL,
    "location" TEXT,
    "purpose" TEXT,
    "outcome" TEXT,
    "attendanceNotes" TEXT,
    "nextAction" TEXT,
    "status" "CourtEventStatus" NOT NULL DEFAULT 'SCHEDULED',
    "createdBy" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "service_events" (
    "id" TEXT NOT NULL,
    "proceedingId" TEXT NOT NULL,
    "recipientName" TEXT NOT NULL,
    "documentDescription" TEXT NOT NULL,
    "method" TEXT NOT NULL,
    "servedAt" TIMESTAMP(3),
    "status" "ServiceStatus" NOT NULL DEFAULT 'PENDING',
    "affidavitReference" TEXT,
    "attempts" INTEGER NOT NULL DEFAULT 0,
    "notes" TEXT,
    "createdBy" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "court_orders" (
    "id" TEXT NOT NULL,
    "proceedingId" TEXT NOT NULL,
    "type" "CourtOrderType" NOT NULL,
    "title" TEXT NOT NULL,
    "deliveredAt" TIMESTAMP(3) NOT NULL,
    "summary" TEXT NOT NULL,
    "complianceDueAt" TIMESTAMP(3),
    "monetaryAmount" DECIMAL(14,2),
    "status" "OrderStatus" NOT NULL DEFAULT 'ACTIVE',
    "notes" TEXT,
    "createdBy" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE INDEX "court_proceedings_caseId_status_idx" ON "court_proceedings"("caseId", "status");

-- CreateIndex
CREATE UNIQUE INDEX "court_proceedings_courtName_courtCaseNumber_key" ON "court_proceedings"("courtName", "courtCaseNumber");

-- CreateIndex
CREATE INDEX "court_filings_proceedingId_status_idx" ON "court_filings"("proceedingId", "status");

-- CreateIndex
CREATE INDEX "court_events_proceedingId_scheduledAt_idx" ON "court_events"("proceedingId", "scheduledAt");

-- CreateIndex
CREATE INDEX "court_events_status_scheduledAt_idx" ON "court_events"("status", "scheduledAt");

-- CreateIndex
CREATE INDEX "service_events_proceedingId_status_idx" ON "service_events"("proceedingId", "status");

-- CreateIndex
CREATE INDEX "court_orders_proceedingId_deliveredAt_idx" ON "court_orders"("proceedingId", "deliveredAt");

-- AddForeignKey
ALTER TABLE "court_proceedings" ADD CONSTRAINT "court_proceedings_caseId_fkey" FOREIGN KEY ("caseId") REFERENCES "cases"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "court_filings" ADD CONSTRAINT "court_filings_proceedingId_fkey" FOREIGN KEY ("proceedingId") REFERENCES "court_proceedings"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "court_events" ADD CONSTRAINT "court_events_proceedingId_fkey" FOREIGN KEY ("proceedingId") REFERENCES "court_proceedings"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "service_events" ADD CONSTRAINT "service_events_proceedingId_fkey" FOREIGN KEY ("proceedingId") REFERENCES "court_proceedings"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "court_orders" ADD CONSTRAINT "court_orders_proceedingId_fkey" FOREIGN KEY ("proceedingId") REFERENCES "court_proceedings"("id") ON DELETE CASCADE ON UPDATE CASCADE;
