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

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

  it("returns a stable assistant error when a vacation 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: "approve_vacation:vac_missing",
            taskStatus: "OPEN",
          }),
        },
        vacation: {
          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: "Vacation not found with the given criteria.",
    });
  });

  it("returns a stable assistant error when a vacation task action is no longer pending", 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: "approve_vacation:vac_1",
            taskStatus: "OPEN",
          }),
        },
        vacation: {
          findUnique: vi.fn().mockResolvedValue({
            id: "vac_1",
            status: VacationStatus.APPROVED,
          }),
        },
      },
      { userRole: SystemRole.ADMIN },
    );

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

    expect(JSON.parse(result.content)).toEqual({
      error: "Vacation is not pending and cannot be approved or rejected via this task action.",
    });
  });
});
