-- CreateEnum
CREATE TYPE "IiosTicketState" AS ENUM ('NEW', 'OPEN', 'PENDING_CUSTOMER', 'PENDING_INTERNAL', 'RESOLVED', 'CLOSED', 'CANCELLED');

-- CreateEnum
CREATE TYPE "IiosTicketPriority" AS ENUM ('P0', 'P1', 'P2', 'P3', 'P4');

-- CreateEnum
CREATE TYPE "IiosCallbackStatus" AS ENUM ('PENDING', 'SCHEDULED', 'COMPLETED', 'MISSED', 'CANCELLED');

-- CreateTable
CREATE TABLE "IiosSupportQueue" (
    "id" TEXT NOT NULL,
    "scopeId" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "supportTier" TEXT NOT NULL DEFAULT 'REGULAR',
    "assignmentPolicy" JSONB,
    "enabled" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "IiosSupportTeamMember" (
    "queueId" TEXT NOT NULL,
    "actorId" TEXT NOT NULL,
    "role" TEXT NOT NULL DEFAULT 'AGENT',
    "maxActive" INTEGER NOT NULL DEFAULT 3,
    "activeCount" INTEGER NOT NULL DEFAULT 0,
    "availabilityState" TEXT NOT NULL DEFAULT 'OFFLINE',

    CONSTRAINT "IiosSupportTeamMember_pkey" PRIMARY KEY ("queueId","actorId")
);

-- CreateTable
CREATE TABLE "IiosTicket" (
    "id" TEXT NOT NULL,
    "scopeId" TEXT NOT NULL,
    "queueId" TEXT,
    "requesterActorId" TEXT,
    "assignedActorId" TEXT,
    "subject" TEXT NOT NULL,
    "state" "IiosTicketState" NOT NULL DEFAULT 'NEW',
    "priority" "IiosTicketPriority" NOT NULL DEFAULT 'P3',
    "issueClass" TEXT,
    "createdFromInteractionId" TEXT,
    "traceId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "metadata" JSONB,

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

-- CreateTable
CREATE TABLE "IiosTicketThreadLink" (
    "ticketId" TEXT NOT NULL,
    "threadId" TEXT NOT NULL,
    "relationKind" TEXT NOT NULL DEFAULT 'PRIMARY',
    "linkedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "IiosTicketThreadLink_pkey" PRIMARY KEY ("ticketId","threadId")
);

-- CreateTable
CREATE TABLE "IiosTicketStateHistory" (
    "id" TEXT NOT NULL,
    "ticketId" TEXT NOT NULL,
    "fromState" "IiosTicketState",
    "toState" "IiosTicketState" NOT NULL,
    "actorId" TEXT,
    "reasonCode" TEXT,
    "at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "IiosCallbackRequest" (
    "id" TEXT NOT NULL,
    "scopeId" TEXT NOT NULL,
    "ticketId" TEXT,
    "requesterActorId" TEXT NOT NULL,
    "preferChannel" TEXT NOT NULL,
    "preferredTime" TIMESTAMP(3),
    "notes" TEXT,
    "status" "IiosCallbackStatus" NOT NULL DEFAULT 'PENDING',
    "meetingRef" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE INDEX "IiosSupportQueue_scopeId_idx" ON "IiosSupportQueue"("scopeId");

-- CreateIndex
CREATE INDEX "IiosSupportTeamMember_actorId_idx" ON "IiosSupportTeamMember"("actorId");

-- CreateIndex
CREATE INDEX "IiosTicket_scopeId_state_idx" ON "IiosTicket"("scopeId", "state");

-- CreateIndex
CREATE INDEX "IiosTicket_assignedActorId_state_idx" ON "IiosTicket"("assignedActorId", "state");

-- CreateIndex
CREATE INDEX "IiosTicketThreadLink_threadId_idx" ON "IiosTicketThreadLink"("threadId");

-- CreateIndex
CREATE INDEX "IiosTicketStateHistory_ticketId_idx" ON "IiosTicketStateHistory"("ticketId");

-- CreateIndex
CREATE INDEX "IiosCallbackRequest_scopeId_status_idx" ON "IiosCallbackRequest"("scopeId", "status");

-- AddForeignKey
ALTER TABLE "IiosSupportQueue" ADD CONSTRAINT "IiosSupportQueue_scopeId_fkey" FOREIGN KEY ("scopeId") REFERENCES "IiosScope"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IiosSupportTeamMember" ADD CONSTRAINT "IiosSupportTeamMember_queueId_fkey" FOREIGN KEY ("queueId") REFERENCES "IiosSupportQueue"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IiosSupportTeamMember" ADD CONSTRAINT "IiosSupportTeamMember_actorId_fkey" FOREIGN KEY ("actorId") REFERENCES "IiosActorRef"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IiosTicket" ADD CONSTRAINT "IiosTicket_scopeId_fkey" FOREIGN KEY ("scopeId") REFERENCES "IiosScope"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IiosTicket" ADD CONSTRAINT "IiosTicket_queueId_fkey" FOREIGN KEY ("queueId") REFERENCES "IiosSupportQueue"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IiosTicket" ADD CONSTRAINT "IiosTicket_requesterActorId_fkey" FOREIGN KEY ("requesterActorId") REFERENCES "IiosActorRef"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IiosTicket" ADD CONSTRAINT "IiosTicket_assignedActorId_fkey" FOREIGN KEY ("assignedActorId") REFERENCES "IiosActorRef"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IiosTicket" ADD CONSTRAINT "IiosTicket_createdFromInteractionId_fkey" FOREIGN KEY ("createdFromInteractionId") REFERENCES "IiosInteraction"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IiosTicketThreadLink" ADD CONSTRAINT "IiosTicketThreadLink_ticketId_fkey" FOREIGN KEY ("ticketId") REFERENCES "IiosTicket"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IiosTicketThreadLink" ADD CONSTRAINT "IiosTicketThreadLink_threadId_fkey" FOREIGN KEY ("threadId") REFERENCES "IiosThread"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IiosTicketStateHistory" ADD CONSTRAINT "IiosTicketStateHistory_ticketId_fkey" FOREIGN KEY ("ticketId") REFERENCES "IiosTicket"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IiosCallbackRequest" ADD CONSTRAINT "IiosCallbackRequest_scopeId_fkey" FOREIGN KEY ("scopeId") REFERENCES "IiosScope"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IiosCallbackRequest" ADD CONSTRAINT "IiosCallbackRequest_ticketId_fkey" FOREIGN KEY ("ticketId") REFERENCES "IiosTicket"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IiosCallbackRequest" ADD CONSTRAINT "IiosCallbackRequest_requesterActorId_fkey" FOREIGN KEY ("requesterActorId") REFERENCES "IiosActorRef"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
