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

vi.mock("@capakraken/application", async (importOriginal) => {
  const actual = await importOriginal<typeof import("@capakraken/application")>();
  return {
    ...actual,
    approveEstimateVersion: vi.fn(),
    cloneEstimate: vi.fn(),
    commitDispoImportBatch: vi.fn(),
    countPlanningEntries: vi.fn().mockResolvedValue({ countsByRoleId: new Map() }),
    createEstimateExport: vi.fn(),
    createEstimatePlanningHandoff: vi.fn(),
    createEstimateRevision: vi.fn(),
    assessDispoImportReadiness: vi.fn(),
    loadResourceDailyAvailabilityContexts: vi.fn().mockResolvedValue(new Map()),
    getDashboardDemand: vi.fn().mockResolvedValue([]),
    getDashboardBudgetForecast: vi.fn().mockResolvedValue([]),
    getDashboardOverview: vi.fn(),
    getDashboardSkillGapSummary: vi.fn().mockResolvedValue({
      roleGaps: [],
      totalOpenPositions: 0,
      skillSupplyTop10: [],
      resourcesByRole: [],
    }),
    getDashboardProjectHealth: vi.fn().mockResolvedValue([]),
    getDashboardPeakTimes: vi.fn().mockResolvedValue([]),
    getDashboardTopValueResources: vi.fn().mockResolvedValue([]),
    getEstimateById: vi.fn(),
    listAssignmentBookings: vi.fn().mockResolvedValue([]),
    stageDispoImportBatch: vi.fn(),
    submitEstimateVersion: vi.fn(),
    updateEstimateDraft: vi.fn(),
  };
});

vi.mock("../ai-client.js", async (importOriginal) => {
  const actual = await importOriginal<typeof import("../ai-client.js")>();
  return {
    ...actual,
    createDalleClient: vi.fn(() => ({
      images: {
        generate: vi.fn().mockResolvedValue({
          data: [{ b64_json: "ZmFrZQ==" }],
        }),
      },
    })),
    loggedAiCall: vi.fn(async (_provider, _model, _promptLength, fn) => fn()),
  };
});

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

describe("assistant project cover removal tools", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("routes project cover removal through the real project router path", async () => {
    const projectFindUnique = vi.fn()
      .mockResolvedValueOnce({
        id: "project_1",
        name: "Project One",
        shortCode: "PROJ-1",
        status: "ACTIVE",
        orderType: "CHARGEABLE",
        allocationType: "INT",
        budgetCents: 0,
        winProbability: 100,
        startDate: new Date("2026-05-01T00:00:00.000Z"),
        endDate: new Date("2026-06-30T00:00:00.000Z"),
        responsiblePerson: "Peter Parker",
        client: null,
        utilizationCategory: null,
        _count: { assignments: 0, estimates: 0 },
      })
      .mockResolvedValueOnce({
        id: "project_1",
        name: "Project One",
      });
    const projectUpdate = vi.fn().mockResolvedValue({
      id: "project_1",
      coverImageUrl: null,
    });
    const ctx = createToolContext(
      {
        project: {
          findUnique: projectFindUnique,
          update: projectUpdate,
        },
        assignment: {
          findMany: vi.fn().mockResolvedValue([]),
        },
      },
      {
        userRole: SystemRole.ADMIN,
        permissions: [PermissionKey.MANAGE_PROJECTS],
      },
    );

    const result = await executeTool(
      "remove_project_cover",
      JSON.stringify({ projectId: "project_1" }),
      ctx,
    );

    expect(projectUpdate).toHaveBeenCalledWith({
      where: { id: "project_1" },
      data: { coverImageUrl: null },
    });
    expect(projectFindUnique).toHaveBeenCalledWith({
      where: { id: "project_1" },
      select: expect.objectContaining({
        id: true,
        shortCode: true,
        name: true,
        status: true,
        responsiblePerson: true,
        startDate: true,
        endDate: true,
      }),
    });
    expect(JSON.parse(result.content)).toEqual(
      expect.objectContaining({
        success: true,
        message: 'Removed cover art from project "Project One"',
      }),
    );
  });
});
