-- Add task priority, timestamps, and Tasks tab permissions

ALTER TABLE "lab_case_tasks" ADD COLUMN "priority" INTEGER NOT NULL DEFAULT 3;
ALTER TABLE "lab_case_tasks" ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP;
ALTER TABLE "lab_case_tasks" ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP;

CREATE INDEX "lab_case_tasks_assigneeUserId_priority_createdAt_idx"
  ON "lab_case_tasks"("assigneeUserId", "priority", "createdAt");

INSERT INTO "features" ("id", "name", "description", "organizationTypeId")
VALUES (gen_random_uuid(), 'Tasks', 'Lab task inbox', NULL)
ON CONFLICT ("name") DO NOTHING;

INSERT INTO "permissions" ("id", "name", "description", "featureId")
SELECT gen_random_uuid(), v.name, NULL, f.id
FROM (VALUES
  ('TAB_TASKS_READ'),
  ('TAB_TASKS_EDIT')
) AS v(name)
CROSS JOIN "features" f
WHERE f.name = 'Tasks'
ON CONFLICT ("name") DO NOTHING;
