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

describe("assistant resource admin create tools - validation errors", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("returns a stable assistant error when the role cannot be resolved during resource creation", async () => {
    const resourceCreate = vi.fn();
    const ctx = createToolContext(
      {
        resource: {
          findFirst: vi.fn().mockResolvedValue(null),
          create: resourceCreate,
        },
        role: {
          findUnique: vi.fn().mockResolvedValue(null),
          findFirst: vi.fn().mockResolvedValue(null),
        },
        assignment: {
          findMany: vi.fn().mockResolvedValue([]),
        },
        demandRequirement: {
          findMany: vi.fn().mockResolvedValue([]),
        },
      },
      {
        userRole: SystemRole.ADMIN,
        permissions: [PermissionKey.MANAGE_RESOURCES],
      },
    );

    const result = await executeTool(
      "create_resource",
      JSON.stringify({
        eid: "EMP-404",
        displayName: "Role Missing",
        email: "missing@example.com",
        lcrCents: 8000,
        roleName: "Missing Role",
      }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual({
      error: 'Role not found: "Missing Role"',
    });
    expect(resourceCreate).not.toHaveBeenCalled();
  });

  it("returns a stable assistant error when creating a duplicate resource", async () => {
    const resourceCreate = vi.fn();
    const ctx = createToolContext(
      {
        resource: {
          findFirst: vi.fn().mockResolvedValue({
            id: "resource_existing",
            eid: "EMP-001",
            email: "carol@example.com",
          }),
          create: resourceCreate,
        },
        assignment: {
          findMany: vi.fn().mockResolvedValue([]),
        },
        demandRequirement: {
          findMany: vi.fn().mockResolvedValue([]),
        },
      },
      {
        userRole: SystemRole.ADMIN,
        permissions: [PermissionKey.MANAGE_RESOURCES],
      },
    );

    const result = await executeTool(
      "create_resource",
      JSON.stringify({
        eid: "EMP-001",
        displayName: "Carol Danvers",
        email: "carol@example.com",
        lcrCents: 8000,
      }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual({
      error: "A resource with this EID or email already exists.",
    });
    expect(resourceCreate).not.toHaveBeenCalled();
  });

  it("requires an email address before creating a resource", async () => {
    const resourceCreate = vi.fn();
    const roleFindUnique = vi.fn();
    const ctx = createToolContext(
      {
        resource: {
          findFirst: vi.fn().mockResolvedValue(null),
          create: resourceCreate,
        },
        role: {
          findUnique: roleFindUnique,
          findFirst: vi.fn().mockResolvedValue(null),
        },
        assignment: {
          findMany: vi.fn().mockResolvedValue([]),
        },
        demandRequirement: {
          findMany: vi.fn().mockResolvedValue([]),
        },
      },
      {
        userRole: SystemRole.ADMIN,
        permissions: [PermissionKey.MANAGE_RESOURCES],
      },
    );

    const result = await executeTool(
      "create_resource",
      JSON.stringify({
        eid: "EMP-NO-MAIL",
        displayName: "Missing Email",
        lcrCents: 8000,
      }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual({
      error: "email is required to create a resource.",
    });
    expect(roleFindUnique).not.toHaveBeenCalled();
    expect(resourceCreate).not.toHaveBeenCalled();
  });
});
