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 - guards", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("returns a stable assistant error when executing a missing task action", async () => {
    const ctx = createToolContext(
      {
        user: {
          findUnique: vi.fn().mockResolvedValue({ id: "user_1" }),
        },
        notification: {
          findFirst: vi.fn().mockResolvedValue(null),
        },
      },
      { userRole: SystemRole.ADMIN },
    );

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

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

  it("returns a stable assistant error when executing an already completed task action", 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: "DONE",
          }),
        },
      },
      { userRole: SystemRole.ADMIN },
    );

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

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

  it("returns a stable assistant error when executing a dismissed task action", 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: "DISMISSED",
          }),
        },
      },
      { userRole: SystemRole.ADMIN },
    );

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

    expect(JSON.parse(result.content)).toEqual({
      error: "Task has been dismissed and cannot be executed.",
    });
  });

  it("returns a stable assistant error when a task has no executable action", 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: null,
            taskStatus: "OPEN",
          }),
        },
      },
      { userRole: SystemRole.ADMIN },
    );

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

    expect(JSON.parse(result.content)).toEqual({
      error: "Task has no executable action.",
    });
  });

  it("returns a stable assistant error when a task action format is invalid", 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: "not-a-valid-task-action",
            taskStatus: "OPEN",
          }),
        },
      },
      { userRole: SystemRole.ADMIN },
    );

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

    expect(JSON.parse(result.content)).toEqual({
      error: "Task action is invalid and cannot be executed.",
    });
  });

  it("returns a stable assistant error when executing a task action without permission", 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",
          }),
        },
      },
      { userRole: SystemRole.USER },
    );

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

    expect(JSON.parse(result.content)).toEqual({
      error: "You do not have permission to execute this task action.",
    });
  });
});
