-- CreateEnum
CREATE TYPE "TreatmentStatus" AS ENUM ('DRAFT', 'COMPLETED');

-- CreateTable
CREATE TABLE "treatments" (
    "id" TEXT NOT NULL,
    "organizationId" TEXT NOT NULL,
    "patientId" TEXT NOT NULL,
    "appointmentId" TEXT,
    "providerUserId" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "status" "TreatmentStatus" NOT NULL DEFAULT 'DRAFT',
    "treatmentAt" TIMESTAMP(3) NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "treatment_cases" (
    "id" TEXT NOT NULL,
    "treatmentId" TEXT NOT NULL,
    "clientKey" TEXT,
    "sortOrder" INTEGER NOT NULL,
    "treatmentType" TEXT NOT NULL,
    "teeth" JSONB NOT NULL,
    "comment" TEXT,
    "sentAt" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "treatment_case_attachments" (
    "id" TEXT NOT NULL,
    "caseId" TEXT,
    "appointmentId" TEXT,
    "caseClientKey" TEXT,
    "fileName" TEXT NOT NULL,
    "mimeType" TEXT NOT NULL,
    "sizeBytes" INTEGER NOT NULL,
    "storagePath" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "treatment_case_sends" (
    "id" TEXT NOT NULL,
    "caseId" TEXT NOT NULL,
    "organizationId" TEXT NOT NULL,
    "sentAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- Migrate legacy patient_treatment_histories into treatments + single case per row
INSERT INTO "treatments" (
    "id",
    "organizationId",
    "patientId",
    "appointmentId",
    "providerUserId",
    "title",
    "status",
    "treatmentAt",
    "createdAt",
    "updatedAt"
)
SELECT
    h."id",
    p."organizationId",
    h."patientId",
    NULL,
    o."ownerId",
    h."title",
    'COMPLETED'::"TreatmentStatus",
    h."treatmentAt",
    h."createdAt",
    h."updatedAt"
FROM "patient_treatment_histories" h
JOIN "patients" p ON p."id" = h."patientId"
JOIN "organizations" o ON o."id" = p."organizationId";

INSERT INTO "treatment_cases" (
    "id",
    "treatmentId",
    "clientKey",
    "sortOrder",
    "treatmentType",
    "teeth",
    "comment",
    "sentAt"
)
SELECT
    h."id" || '-case',
    h."id",
    NULL,
    0,
    'visit',
    CASE
        WHEN h."tooth" IS NOT NULL AND btrim(h."tooth") <> '' THEN jsonb_build_array(h."tooth")
        ELSE '[]'::jsonb
    END,
    h."notes",
    NULL
FROM "patient_treatment_histories" h;

-- Drop legacy table
DROP TABLE "patient_treatment_histories";

-- CreateIndex
CREATE UNIQUE INDEX "treatments_appointmentId_key" ON "treatments"("appointmentId");

-- CreateIndex
CREATE INDEX "treatments_patientId_treatmentAt_idx" ON "treatments"("patientId", "treatmentAt");

-- CreateIndex
CREATE INDEX "treatments_organizationId_status_idx" ON "treatments"("organizationId", "status");

-- CreateIndex
CREATE INDEX "treatment_cases_treatmentId_sortOrder_idx" ON "treatment_cases"("treatmentId", "sortOrder");

-- CreateIndex
CREATE INDEX "treatment_case_attachments_appointmentId_caseClientKey_idx" ON "treatment_case_attachments"("appointmentId", "caseClientKey");

-- CreateIndex
CREATE INDEX "treatment_case_attachments_caseId_idx" ON "treatment_case_attachments"("caseId");

-- CreateIndex
CREATE UNIQUE INDEX "treatment_case_sends_caseId_organizationId_key" ON "treatment_case_sends"("caseId", "organizationId");

-- AddForeignKey
ALTER TABLE "treatments" ADD CONSTRAINT "treatments_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "organizations"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "treatments" ADD CONSTRAINT "treatments_patientId_fkey" FOREIGN KEY ("patientId") REFERENCES "patients"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "treatments" ADD CONSTRAINT "treatments_appointmentId_fkey" FOREIGN KEY ("appointmentId") REFERENCES "appointments"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "treatment_cases" ADD CONSTRAINT "treatment_cases_treatmentId_fkey" FOREIGN KEY ("treatmentId") REFERENCES "treatments"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "treatment_case_attachments" ADD CONSTRAINT "treatment_case_attachments_caseId_fkey" FOREIGN KEY ("caseId") REFERENCES "treatment_cases"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "treatment_case_sends" ADD CONSTRAINT "treatment_case_sends_caseId_fkey" FOREIGN KEY ("caseId") REFERENCES "treatment_cases"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "treatment_case_sends" ADD CONSTRAINT "treatment_case_sends_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "organizations"("id") ON DELETE CASCADE ON UPDATE CASCADE;
