-- CreateEnum
CREATE TYPE "TaskStatus" AS ENUM ('Pending', 'InProgress', 'Validate', 'Complete');

-- AlterTable
ALTER TABLE "RequestWorkStepStatus" ADD COLUMN     "taskOrderId" TEXT;

-- CreateTable
CREATE TABLE "TaskOrder" (
    "id" TEXT NOT NULL,
    "code" TEXT NOT NULL,
    "taskName" TEXT NOT NULL,
    "taskStatus" "TaskStatus" NOT NULL DEFAULT 'Pending',
    "contactName" TEXT NOT NULL,
    "contactTel" TEXT NOT NULL,
    "institutionId" TEXT NOT NULL,
    "acceptedByUserId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "createdByUserId" TEXT NOT NULL,

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

-- AddForeignKey
ALTER TABLE "RequestWorkStepStatus" ADD CONSTRAINT "RequestWorkStepStatus_taskOrderId_fkey" FOREIGN KEY ("taskOrderId") REFERENCES "TaskOrder"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TaskOrder" ADD CONSTRAINT "TaskOrder_institutionId_fkey" FOREIGN KEY ("institutionId") REFERENCES "Institution"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TaskOrder" ADD CONSTRAINT "TaskOrder_acceptedByUserId_fkey" FOREIGN KEY ("acceptedByUserId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TaskOrder" ADD CONSTRAINT "TaskOrder_createdByUserId_fkey" FOREIGN KEY ("createdByUserId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
