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

  it("routes project creation through the real project, blueprint, and client router paths", async () => {
    const auditCreate = vi.fn().mockResolvedValue({ id: "audit_1" });
    const projectCreate = vi.fn().mockResolvedValue({
      id: "project_1",
      shortCode: "PROJ-1",
      name: "Project One",
      status: "DRAFT",
    });
    const ctx = createToolContext(
      {
        resource: {
          findFirst: vi.fn().mockResolvedValue({
            displayName: "Peter Parker",
          }),
          findMany: vi.fn().mockResolvedValue([]),
        },
        project: {
          findUnique: vi.fn().mockResolvedValue(null),
          create: projectCreate,
        },
        blueprint: {
          findUnique: vi.fn().mockResolvedValue({
            id: "bp_1",
            name: "Consulting Blueprint",
            target: "PROJECT",
            fieldDefs: [],
          }),
          findFirst: vi.fn().mockResolvedValue(null),
        },
        client: {
          findUnique: vi.fn().mockResolvedValue({
            id: "client_1",
            name: "Acme",
            code: "ACME",
            _count: { projects: 0, children: 0 },
          }),
          findFirst: vi.fn().mockResolvedValue(null),
        },
        webhook: {
          findMany: vi.fn().mockResolvedValue([]),
        },
        auditLog: {
          create: auditCreate,
        },
      },
      {
        userRole: SystemRole.ADMIN,
        permissions: [PermissionKey.MANAGE_PROJECTS],
      },
    );

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

    expect(JSON.parse(result.content)).toEqual(
      expect.objectContaining({
        success: true,
        message: expect.stringContaining("Created project: Project One (PROJ-1), budget "),
        projectId: "project_1",
      }),
    );
    expect(projectCreate).toHaveBeenCalledWith(
      expect.objectContaining({
        data: expect.objectContaining({
          shortCode: "PROJ-1",
          blueprintId: "bp_1",
          clientId: "client_1",
          responsiblePerson: "Peter Parker",
        }),
      }),
    );
    expect(auditCreate).toHaveBeenCalledTimes(1);
  });

  it("applies assistant-side default project fields when optional create inputs are omitted", async () => {
    const projectCreate = vi.fn().mockResolvedValue({
      id: "project_2",
      shortCode: "PROJ-DEFAULTS",
      name: "Project Defaults",
      status: "DRAFT",
    });
    const ctx = createToolContext(
      {
        resource: {
          findFirst: vi.fn().mockResolvedValue({
            displayName: "Peter Parker",
          }),
          findMany: vi.fn().mockResolvedValue([]),
        },
        project: {
          findUnique: vi.fn().mockResolvedValue(null),
          create: projectCreate,
        },
        auditLog: {
          create: vi.fn().mockResolvedValue({ id: "audit_2" }),
        },
      },
      {
        userRole: SystemRole.ADMIN,
        permissions: [PermissionKey.MANAGE_PROJECTS],
      },
    );

    const result = await executeTool(
      "create_project",
      JSON.stringify({
        shortCode: "PROJ-DEFAULTS",
        name: "Project Defaults",
        orderType: "CHARGEABLE",
        budgetCents: 250000,
        startDate: "2026-07-01",
        endDate: "2026-07-31",
        responsiblePerson: "Peter Parker",
      }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual(
      expect.objectContaining({
        success: true,
        projectId: "project_2",
        shortCode: "PROJ-DEFAULTS",
      }),
    );
    expect(projectCreate).toHaveBeenCalledWith(
      expect.objectContaining({
        data: expect.objectContaining({
          allocationType: "INT",
          winProbability: 100,
          status: "DRAFT",
          staffingReqs: [],
          dynamicFields: {},
          responsiblePerson: "Peter Parker",
        }),
      }),
    );
  });
});
