-- CreateEnum
CREATE TYPE "LeadStatus" AS ENUM ('NEW', 'CONTACTING', 'INTAKE_IN_PROGRESS', 'ADVOCATE_REVIEW', 'CONFLICT_REVIEW', 'ENGAGEMENT_PENDING', 'QUALIFIED', 'CONVERTED', 'DECLINED', 'REFERRED');

-- CreateEnum
CREATE TYPE "ContactChannel" AS ENUM ('PHONE', 'EMAIL', 'PORTAL', 'IN_PERSON', 'OTHER');

-- CreateEnum
CREATE TYPE "ContactOutcome" AS ENUM ('NO_ANSWER', 'CONNECTED', 'APPOINTMENT_SET', 'INFORMATION_REQUESTED', 'NOT_INTERESTED', 'WRONG_CONTACT', 'OTHER');

-- CreateTable
CREATE TABLE "leads" (
    "id" TEXT NOT NULL,
    "reference" TEXT NOT NULL,
    "status" "LeadStatus" NOT NULL DEFAULT 'NEW',
    "source" TEXT,
    "ownerId" TEXT,
    "firstName" TEXT NOT NULL,
    "lastName" TEXT NOT NULL,
    "email" TEXT,
    "phone" TEXT NOT NULL,
    "preferredContact" TEXT,
    "practiceArea" "PracticeArea" NOT NULL DEFAULT 'OTHER',
    "incidentDate" TIMESTAMP(3),
    "incidentLocation" TEXT,
    "incidentSummary" TEXT NOT NULL,
    "injurySummary" TEXT,
    "treatmentSummary" TEXT,
    "policeReference" TEXT,
    "insurerDetails" TEXT,
    "employmentImpact" TEXT,
    "limitationConcern" TEXT,
    "urgency" "Priority" NOT NULL DEFAULT 'MEDIUM',
    "qualificationNotes" TEXT,
    "decisionReason" TEXT,
    "consentToContact" BOOLEAN NOT NULL DEFAULT false,
    "privacyNoticeVersion" TEXT,
    "convertedCaseId" TEXT,
    "convertedAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "lead_contact_attempts" (
    "id" TEXT NOT NULL,
    "leadId" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "channel" "ContactChannel" NOT NULL,
    "outcome" "ContactOutcome" NOT NULL,
    "notes" TEXT,
    "contactedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "followUpAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE UNIQUE INDEX "leads_reference_key" ON "leads"("reference");

-- CreateIndex
CREATE UNIQUE INDEX "leads_convertedCaseId_key" ON "leads"("convertedCaseId");

-- CreateIndex
CREATE INDEX "leads_status_createdAt_idx" ON "leads"("status", "createdAt");

-- CreateIndex
CREATE INDEX "leads_ownerId_status_idx" ON "leads"("ownerId", "status");

-- CreateIndex
CREATE INDEX "leads_email_idx" ON "leads"("email");

-- CreateIndex
CREATE INDEX "leads_phone_idx" ON "leads"("phone");

-- CreateIndex
CREATE INDEX "lead_contact_attempts_leadId_contactedAt_idx" ON "lead_contact_attempts"("leadId", "contactedAt");

-- CreateIndex
CREATE INDEX "lead_contact_attempts_followUpAt_idx" ON "lead_contact_attempts"("followUpAt");

-- AddForeignKey
ALTER TABLE "leads" ADD CONSTRAINT "leads_ownerId_fkey" FOREIGN KEY ("ownerId") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "leads" ADD CONSTRAINT "leads_convertedCaseId_fkey" FOREIGN KEY ("convertedCaseId") REFERENCES "cases"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "lead_contact_attempts" ADD CONSTRAINT "lead_contact_attempts_leadId_fkey" FOREIGN KEY ("leadId") REFERENCES "leads"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "lead_contact_attempts" ADD CONSTRAINT "lead_contact_attempts_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
