import { beforeEach, describe, expect, it, vi } from "vitest";
import { SystemRole } from "@capakraken/shared";
import {
  createToolContext,
  executeTool,
} from "./assistant-tools-task-action-test-helpers.js";

describe("assistant task action tools - assignment errors", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("returns a stable assistant error when an assignment task action target disappears", async () => {
    const ctx = createToolContext(
      {
        user: {
          findUnique: vi.fn().mockResolvedValue({ id: "user_1" }),
        },
        notification: {
          findFirst: vi.fn().mockResolvedValue({
            id: "task_1",
            userId: "user_1",
            assigneeId: null,
            taskAction: "confirm_assignment:asg_missing",
            taskStatus: "OPEN",
          }),
        },
        assignment: {
          findUnique: vi.fn().mockResolvedValue(null),
        },
      },
      { userRole: SystemRole.ADMIN },
    );

    const result = await executeTool(
      "execute_task_action",
      JSON.stringify({ taskId: "task_1" }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual({
      error: "Assignment not found with the given criteria.",
    });
  });

  it("returns a stable assistant error when an assignment is already confirmed", async () => {
    const ctx = createToolContext(
      {
        user: {
          findUnique: vi.fn().mockResolvedValue({ id: "user_1" }),
        },
        notification: {
          findFirst: vi.fn().mockResolvedValue({
            id: "task_1",
            userId: "user_1",
            assigneeId: null,
            taskAction: "confirm_assignment:asg_1",
            taskStatus: "OPEN",
          }),
        },
        assignment: {
          findUnique: vi.fn().mockResolvedValue({
            id: "asg_1",
            status: "CONFIRMED",
          }),
        },
      },
      { userRole: SystemRole.ADMIN },
    );

    const result = await executeTool(
      "execute_task_action",
      JSON.stringify({ taskId: "task_1" }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual({
      error: "Assignment is already confirmed.",
    });
  });
});
