import { beforeEach, describe, expect, it, vi } from "vitest";
import { SystemRole } from "@capakraken/shared";

import { createToolContext, executeTool } from "./assistant-tools-comments-test-helpers.js";

describe("assistant comment tools resolve errors", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("returns a stable assistant error when resolving a missing comment", async () => {
    const ctx = createToolContext(
      {
        user: {
          findUnique: vi.fn().mockResolvedValue({ id: "user_1" }),
        },
        comment: {
          findUnique: vi.fn().mockResolvedValue(null),
        },
      },
      { userRole: SystemRole.CONTROLLER },
    );

    const result = await executeTool(
      "resolve_comment",
      JSON.stringify({ commentId: "comment_missing", resolved: true }),
      ctx,
    );

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

  it("returns a stable assistant error when a non-author resolves a comment", async () => {
    const ctx = createToolContext(
      {
        user: {
          findUnique: vi.fn().mockResolvedValue({ id: "user_1" }),
        },
        comment: {
          findUnique: vi.fn().mockResolvedValue({
            id: "comment_1",
            authorId: "user_2",
            entityType: "estimate",
            entityId: "est_1",
          }),
        },
        estimate: {
          findUnique: vi.fn().mockResolvedValue({ id: "est_1" }),
        },
      },
      { userRole: SystemRole.CONTROLLER },
    );

    const result = await executeTool(
      "resolve_comment",
      JSON.stringify({ commentId: "comment_1", resolved: true }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual({
      error: "Only the comment author or an admin can resolve comments.",
    });
  });
});
