import { beforeEach, describe, expect, it, vi } from "vitest";
import { ImportBatchStatus } from "@capakraken/db";
import { 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(),
  };
});

import {
  assessDispoImportReadiness,
  commitDispoImportBatch,
  stageDispoImportBatch,
} from "@capakraken/application";
import { executeTool } from "../router/assistant-tools.js";
import { createToolContext } from "./assistant-tools-dispo-test-helpers.js";

describe("assistant dispo import batch delegation tools", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("delegates dispo import batch staging through the real router path", async () => {
    vi.mocked(stageDispoImportBatch).mockResolvedValue({
      id: "batch_1",
      status: ImportBatchStatus.STAGED,
    } as never);
    const ctx = createToolContext({}, { userRole: SystemRole.ADMIN });

    const result = await executeTool(
      "stage_dispo_import_batch",
      JSON.stringify({
        chargeabilityWorkbookPath: "/imports/chargeability.xlsx",
        planningWorkbookPath: "/imports/planning.xlsx",
        referenceWorkbookPath: "/imports/reference.xlsx",
        costWorkbookPath: "/imports/cost.xlsx",
        rosterWorkbookPath: "/imports/roster.xlsx",
        notes: "March import",
      }),
      ctx,
    );

    expect(stageDispoImportBatch).toHaveBeenCalledWith(ctx.db, {
      chargeabilityWorkbookPath: "/imports/chargeability.xlsx",
      planningWorkbookPath: "/imports/planning.xlsx",
      referenceWorkbookPath: "/imports/reference.xlsx",
      costWorkbookPath: "/imports/cost.xlsx",
      rosterWorkbookPath: "/imports/roster.xlsx",
      notes: "March import",
    });
    expect(JSON.parse(result.content)).toEqual({
      id: "batch_1",
      status: ImportBatchStatus.STAGED,
    });
  });

  it("delegates dispo import batch validation through the real router path", async () => {
    vi.mocked(assessDispoImportReadiness).mockResolvedValue({
      ready: false,
      checks: [{ key: "stagedResources", status: "warning" }],
    } as never);
    const ctx = createToolContext({}, { userRole: SystemRole.ADMIN });

    const result = await executeTool(
      "validate_dispo_import_batch",
      JSON.stringify({
        chargeabilityWorkbookPath: "/imports/chargeability.xlsx",
        planningWorkbookPath: "/imports/planning.xlsx",
        referenceWorkbookPath: "/imports/reference.xlsx",
        importBatchId: "batch_1",
      }),
      ctx,
    );

    expect(assessDispoImportReadiness).toHaveBeenCalledWith({
      chargeabilityWorkbookPath: "/imports/chargeability.xlsx",
      planningWorkbookPath: "/imports/planning.xlsx",
      referenceWorkbookPath: "/imports/reference.xlsx",
      importBatchId: "batch_1",
    });
    expect(JSON.parse(result.content)).toEqual({
      ready: false,
      checks: [{ key: "stagedResources", status: "warning" }],
    });
  });

  it("delegates dispo import batch commits through the real router path", async () => {
    vi.mocked(commitDispoImportBatch).mockResolvedValue({
      importedAssignments: 7,
      importedProjects: 3,
    } as never);
    const ctx = createToolContext(
      {
        auditLog: {
          create: vi.fn().mockResolvedValue(undefined),
        },
      },
      { userRole: SystemRole.ADMIN },
    );

    const result = await executeTool(
      "commit_dispo_import_batch",
      JSON.stringify({
        importBatchId: "batch_1",
        allowTbdUnresolved: true,
        importTbdProjects: false,
      }),
      ctx,
    );

    expect(commitDispoImportBatch).toHaveBeenCalledWith(ctx.db, {
      importBatchId: "batch_1",
      allowTbdUnresolved: true,
      importTbdProjects: false,
    });
    expect(JSON.parse(result.content)).toEqual({
      importedAssignments: 7,
      importedProjects: 3,
    });
  });
});
