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

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

  it("returns a stable assistant error when creating a duplicate project short code", async () => {
    const projectCreate = vi.fn();
    const ctx = createToolContext(
      {
        resource: {
          findFirst: vi.fn().mockResolvedValue({
            displayName: "Peter Parker",
          }),
          findMany: vi.fn().mockResolvedValue([]),
        },
        project: {
          findUnique: vi.fn().mockResolvedValue({
            id: "project_existing",
            shortCode: "PROJ-1",
            name: "Existing Project",
          }),
          create: projectCreate,
        },
      },
      {
        userRole: SystemRole.ADMIN,
        permissions: [PermissionKey.MANAGE_PROJECTS],
      },
    );

    const result = await executeTool(
      "create_project",
      JSON.stringify({
        shortCode: "PROJ-1",
        name: "Duplicate Project",
        orderType: "CHARGEABLE",
        budgetCents: 150000,
        startDate: "2026-05-01",
        endDate: "2026-06-30",
        responsiblePerson: "Peter Parker",
      }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual({
      error: 'A project with short code "PROJ-1" already exists.',
    });
    expect(projectCreate).not.toHaveBeenCalled();
  });

  it("requires a responsible person before creating a project", async () => {
    const projectCreate = vi.fn();
    const resourceFindFirst = vi.fn();
    const ctx = createToolContext(
      {
        resource: {
          findFirst: resourceFindFirst,
          findMany: vi.fn().mockResolvedValue([]),
        },
        project: {
          findUnique: vi.fn().mockResolvedValue(null),
          create: projectCreate,
        },
      },
      {
        userRole: SystemRole.ADMIN,
        permissions: [PermissionKey.MANAGE_PROJECTS],
      },
    );

    const result = await executeTool(
      "create_project",
      JSON.stringify({
        shortCode: "PROJ-NO-RP",
        name: "Missing Responsible Person",
        orderType: "CHARGEABLE",
        budgetCents: 150000,
        startDate: "2026-05-01",
        endDate: "2026-06-30",
      }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual({
      error: "responsiblePerson is required to create a project.",
    });
    expect(resourceFindFirst).not.toHaveBeenCalled();
    expect(projectCreate).not.toHaveBeenCalled();
  });

  it("returns the responsible person resolution error when no active resource matches", async () => {
    const projectCreate = vi.fn();
    const ctx = createToolContext(
      {
        resource: {
          findFirst: vi.fn().mockResolvedValue(null),
          findMany: vi.fn().mockResolvedValue([]),
        },
        project: {
          findUnique: vi.fn().mockResolvedValue(null),
          create: projectCreate,
        },
      },
      {
        userRole: SystemRole.ADMIN,
        permissions: [PermissionKey.MANAGE_PROJECTS],
      },
    );

    const result = await executeTool(
      "create_project",
      JSON.stringify({
        shortCode: "PROJ-RP-MISSING",
        name: "Missing Responsible Match",
        orderType: "CHARGEABLE",
        budgetCents: 150000,
        startDate: "2026-05-01",
        endDate: "2026-06-30",
        responsiblePerson: "Mary Jane",
      }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual({
      error: 'No active resource found matching "Mary Jane". The responsible person must be an existing resource.',
    });
    expect(projectCreate).not.toHaveBeenCalled();
  });

  it("returns a stable assistant error when the referenced client cannot be resolved", async () => {
    const projectCreate = vi.fn();
    const ctx = createToolContext(
      {
        resource: {
          findFirst: vi.fn().mockResolvedValue({
            displayName: "Peter Parker",
          }),
          findMany: vi.fn().mockResolvedValue([]),
        },
        project: {
          findUnique: vi.fn().mockResolvedValue(null),
          create: projectCreate,
        },
        client: {
          findUnique: vi.fn().mockResolvedValue(null),
          findFirst: vi.fn().mockResolvedValue(null),
        },
      },
      {
        userRole: SystemRole.ADMIN,
        permissions: [PermissionKey.MANAGE_PROJECTS],
      },
    );

    const result = await executeTool(
      "create_project",
      JSON.stringify({
        shortCode: "PROJ-NO-CLIENT",
        name: "Missing Client",
        orderType: "CHARGEABLE",
        budgetCents: 150000,
        startDate: "2026-05-01",
        endDate: "2026-06-30",
        responsiblePerson: "Peter Parker",
        clientName: "Missing Client",
      }),
      ctx,
    );

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