CREATE TYPE "CaseTeamRole" AS ENUM (
  'LEAD_ADVOCATE',
  'ADVOCATE',
  'PARALEGAL',
  'CLERK',
  'ACCOUNTANT',
  'MEMBER'
);

CREATE TABLE "case_teams" (
  "id" TEXT NOT NULL,
  "caseId" TEXT NOT NULL,
  "userId" TEXT NOT NULL,
  "role" "CaseTeamRole" NOT NULL DEFAULT 'MEMBER',
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL,

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

CREATE TABLE "portal_document_requests" (
  "id" TEXT NOT NULL,
  "clientUserId" TEXT NOT NULL,
  "clientId" TEXT NOT NULL,
  "caseId" TEXT,
  "title" TEXT NOT NULL,
  "description" TEXT,
  "status" TEXT NOT NULL DEFAULT 'OPEN',
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL,

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

CREATE UNIQUE INDEX "case_teams_caseId_userId_key" ON "case_teams"("caseId", "userId");
CREATE INDEX "case_teams_caseId_idx" ON "case_teams"("caseId");
CREATE INDEX "case_teams_userId_idx" ON "case_teams"("userId");
CREATE INDEX "portal_document_requests_clientId_status_idx" ON "portal_document_requests"("clientId", "status");
CREATE INDEX "portal_document_requests_clientUserId_createdAt_idx" ON "portal_document_requests"("clientUserId", "createdAt");

ALTER TABLE "case_teams" ADD CONSTRAINT "case_teams_caseId_fkey" FOREIGN KEY ("caseId") REFERENCES "cases"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "case_teams" ADD CONSTRAINT "case_teams_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "portal_document_requests" ADD CONSTRAINT "portal_document_requests_clientUserId_fkey" FOREIGN KEY ("clientUserId") REFERENCES "client_users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "portal_document_requests" ADD CONSTRAINT "portal_document_requests_clientId_fkey" FOREIGN KEY ("clientId") REFERENCES "clients"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "portal_document_requests" ADD CONSTRAINT "portal_document_requests_caseId_fkey" FOREIGN KEY ("caseId") REFERENCES "cases"("id") ON DELETE SET NULL ON UPDATE CASCADE;
