-- CreateEnum
CREATE TYPE "PracticeArea" AS ENUM ('ROAD_TRAFFIC_ACCIDENT', 'WORKPLACE_INJURY', 'MEDICAL_NEGLIGENCE', 'OCCUPIERS_LIABILITY', 'PRODUCT_LIABILITY', 'PROFESSIONAL_NEGLIGENCE', 'FATAL_CLAIM', 'OTHER');

-- CreateEnum
CREATE TYPE "CaseLifecycleStage" AS ENUM ('LEAD_RECEIVED', 'INTAKE_QUALIFICATION', 'CONFLICT_REVIEW', 'ADVOCATE_REVIEW', 'ENGAGEMENT_PENDING', 'OPEN_INVESTIGATION', 'TREATMENT_EVIDENCE', 'LIABILITY_QUANTUM', 'DEMAND_ISSUED', 'NEGOTIATION', 'LITIGATION', 'JUDGMENT_SETTLEMENT', 'FUNDS_RECEIVED', 'OBLIGATIONS_RESOLUTION', 'CLIENT_DISBURSEMENT', 'CLOSED', 'DECLINED', 'REFERRED');

-- CreateEnum
CREATE TYPE "OrganizationType" AS ENUM ('INSURER', 'MEDICAL_PROVIDER', 'EMPLOYER', 'LAW_FIRM', 'COURT', 'POLICE_STATION', 'EXPERT_FIRM', 'GOVERNMENT', 'OTHER');

-- CreateEnum
CREATE TYPE "ParticipantRole" AS ENUM ('CLAIMANT', 'DEPENDANT', 'DEFENDANT', 'INSURER', 'ADVOCATE', 'WITNESS', 'MEDICAL_PROVIDER', 'EMPLOYER', 'EXPERT', 'INVESTIGATOR', 'OTHER');

-- CreateEnum
CREATE TYPE "TaskStatus" AS ENUM ('OPEN', 'IN_PROGRESS', 'BLOCKED', 'COMPLETED', 'CANCELLED');

-- CreateEnum
CREATE TYPE "DeadlineType" AS ENUM ('LIMITATION', 'COURT_DATE', 'FILING', 'SERVICE', 'RESPONSE', 'DEMAND_EXPIRY', 'INTERNAL', 'OTHER');

-- CreateEnum
CREATE TYPE "DeadlineStatus" AS ENUM ('UNVERIFIED', 'VERIFIED', 'COMPLETED', 'CANCELLED');

-- AlterTable
ALTER TABLE "cases" ADD COLUMN     "archivedAt" TIMESTAMP(3),
ADD COLUMN     "courtCaseNumber" TEXT,
ADD COLUMN     "courtName" TEXT,
ADD COLUMN     "incidentDate" TIMESTAMP(3),
ADD COLUMN     "jurisdiction" TEXT NOT NULL DEFAULT 'Kenya',
ADD COLUMN     "lifecycleStage" "CaseLifecycleStage" NOT NULL DEFAULT 'LEAD_RECEIVED',
ADD COLUMN     "limitationDate" TIMESTAMP(3),
ADD COLUMN     "limitationVerifiedAt" TIMESTAMP(3),
ADD COLUMN     "nextAction" TEXT,
ADD COLUMN     "nextActionDate" TIMESTAMP(3),
ADD COLUMN     "practiceArea" "PracticeArea" NOT NULL DEFAULT 'OTHER',
ADD COLUMN     "source" TEXT;

-- CreateTable
CREATE TABLE "people" (
    "id" TEXT NOT NULL,
    "firstName" TEXT NOT NULL,
    "middleName" TEXT,
    "lastName" TEXT NOT NULL,
    "nationalId" TEXT,
    "passportNumber" TEXT,
    "email" TEXT,
    "phone" TEXT,
    "alternatePhone" TEXT,
    "address" TEXT,
    "dateOfBirth" TIMESTAMP(3),
    "aliases" TEXT[] DEFAULT ARRAY[]::TEXT[],
    "notes" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "organizations" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "type" "OrganizationType" NOT NULL DEFAULT 'OTHER',
    "registrationNumber" TEXT,
    "email" TEXT,
    "phone" TEXT,
    "address" TEXT,
    "aliases" TEXT[] DEFAULT ARRAY[]::TEXT[],
    "notes" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "case_participants" (
    "id" TEXT NOT NULL,
    "caseId" TEXT NOT NULL,
    "personId" TEXT,
    "organizationId" TEXT,
    "role" "ParticipantRole" NOT NULL,
    "isPrimary" BOOLEAN NOT NULL DEFAULT false,
    "notes" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "tasks" (
    "id" TEXT NOT NULL,
    "caseId" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "description" TEXT,
    "status" "TaskStatus" NOT NULL DEFAULT 'OPEN',
    "priority" "Priority" NOT NULL DEFAULT 'MEDIUM',
    "dueAt" TIMESTAMP(3),
    "completedAt" TIMESTAMP(3),
    "assignedTo" TEXT,
    "createdBy" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "deadlines" (
    "id" TEXT NOT NULL,
    "caseId" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "type" "DeadlineType" NOT NULL,
    "dueAt" TIMESTAMP(3) NOT NULL,
    "governingRule" TEXT,
    "calculationNotes" TEXT,
    "isCritical" BOOLEAN NOT NULL DEFAULT false,
    "status" "DeadlineStatus" NOT NULL DEFAULT 'UNVERIFIED',
    "verifiedBy" TEXT,
    "verifiedAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "case_activities" (
    "id" TEXT NOT NULL,
    "caseId" TEXT NOT NULL,
    "userId" TEXT,
    "type" TEXT NOT NULL,
    "summary" TEXT NOT NULL,
    "entityType" TEXT,
    "entityId" TEXT,
    "metadata" JSONB,
    "occurredAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE INDEX "people_lastName_firstName_idx" ON "people"("lastName", "firstName");

-- CreateIndex
CREATE INDEX "people_email_idx" ON "people"("email");

-- CreateIndex
CREATE INDEX "people_phone_idx" ON "people"("phone");

-- CreateIndex
CREATE INDEX "organizations_name_idx" ON "organizations"("name");

-- CreateIndex
CREATE INDEX "case_participants_caseId_role_idx" ON "case_participants"("caseId", "role");

-- CreateIndex
CREATE INDEX "case_participants_personId_idx" ON "case_participants"("personId");

-- CreateIndex
CREATE INDEX "case_participants_organizationId_idx" ON "case_participants"("organizationId");

-- CreateIndex
CREATE INDEX "tasks_caseId_status_idx" ON "tasks"("caseId", "status");

-- CreateIndex
CREATE INDEX "tasks_assignedTo_dueAt_idx" ON "tasks"("assignedTo", "dueAt");

-- CreateIndex
CREATE INDEX "deadlines_caseId_dueAt_idx" ON "deadlines"("caseId", "dueAt");

-- CreateIndex
CREATE INDEX "deadlines_status_dueAt_idx" ON "deadlines"("status", "dueAt");

-- CreateIndex
CREATE INDEX "case_activities_caseId_occurredAt_idx" ON "case_activities"("caseId", "occurredAt");

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

-- AddForeignKey
ALTER TABLE "case_participants" ADD CONSTRAINT "case_participants_personId_fkey" FOREIGN KEY ("personId") REFERENCES "people"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "case_participants" ADD CONSTRAINT "case_participants_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "organizations"("id") ON DELETE SET NULL ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "tasks" ADD CONSTRAINT "tasks_assignedTo_fkey" FOREIGN KEY ("assignedTo") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tasks" ADD CONSTRAINT "tasks_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "deadlines" ADD CONSTRAINT "deadlines_verifiedBy_fkey" FOREIGN KEY ("verifiedBy") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "case_activities" ADD CONSTRAINT "case_activities_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;
