import { VacationType } from "@capakraken/db";
import { vi } from "vitest";

import {
  executeTool as executeAssistantTool,
} from "../router/assistant-tools.js";

export { createToolContext } from "./assistant-tools-vacation-entitlement-test-helpers.js";

export function createHappyPathDb() {
  const vacationFindUnique = vi.fn().mockImplementation(async (args?: any) => {
    const id = args?.where?.id;
    if (id === "vac_cancelled") {
      return {
        id,
        resourceId: "res_1",
        requestedById: "user_1",
        status: "CANCELLED",
        startDate: new Date("2026-07-01T00:00:00.000Z"),
        endDate: new Date("2026-07-02T00:00:00.000Z"),
        type: VacationType.ANNUAL,
        isHalfDay: false,
        resource: {
          id: "res_1",
          displayName: "Alice Example",
          eid: "EMP-001",
          chapter: "Delivery",
        },
        requestedBy: { id: "user_1", name: "Assistant User", email: "assistant@example.com" },
        approvedBy: null,
      };
    }
    if (id === "vac_pending") {
      return {
        id,
        resourceId: "res_1",
        requestedById: "user_1",
        status: "PENDING",
        startDate: new Date("2026-08-03T00:00:00.000Z"),
        endDate: new Date("2026-08-04T00:00:00.000Z"),
        type: VacationType.ANNUAL,
        isHalfDay: false,
        resource: {
          id: "res_1",
          displayName: "Alice Example",
          eid: "EMP-001",
          chapter: "Delivery",
        },
        requestedBy: { id: "user_1", name: "Assistant User", email: "assistant@example.com" },
        approvedBy: null,
      };
    }
    if (id === "vac_self") {
      return {
        id,
        resourceId: "res_1",
        requestedById: "user_1",
        status: "APPROVED",
        startDate: new Date("2026-09-07T00:00:00.000Z"),
        endDate: new Date("2026-09-09T00:00:00.000Z"),
        resource: {
          id: "res_1",
          displayName: "Alice Example",
          eid: "EMP-001",
          chapter: "Delivery",
        },
        requestedBy: { id: "user_1", name: "Assistant User", email: "assistant@example.com" },
        approvedBy: null,
      };
    }
    return null;
  });

  return {
    user: {
      findUnique: vi.fn().mockResolvedValue({ id: "user_1", systemRole: "MANAGER" }),
    },
    resource: {
      findUnique: vi.fn().mockImplementation(async (args?: any) => {
        if (args?.where?.id === "res_1") {
          return {
            id: "res_1",
            eid: "EMP-001",
            displayName: "Alice Example",
            userId: "user_1",
            chapter: "Delivery",
            federalState: "BY",
            countryId: "country_de",
            metroCityId: null,
            country: { code: "DE", name: "Germany" },
            metroCity: null,
          };
        }
        return { userId: "user_1" };
      }),
      count: vi.fn().mockResolvedValue(1),
      findFirst: vi.fn().mockResolvedValue(null),
    },
    holidayCalendar: {
      findMany: vi.fn().mockResolvedValue([]),
    },
    systemSettings: {
      findUnique: vi.fn()
        .mockResolvedValueOnce({ vacationDefaultDays: 30 })
        .mockResolvedValueOnce({ anonymizationEnabled: false }),
    },
    vacationEntitlement: {
      findUnique: vi.fn().mockResolvedValue(null),
      create: vi.fn(),
      update: vi.fn(),
    },
    vacation: {
      findUnique: vacationFindUnique,
      findMany: vi.fn().mockResolvedValue([]),
      findFirst: vi.fn().mockResolvedValue(null),
      create: vi.fn().mockResolvedValue({
        id: "vac_created",
        resourceId: "res_1",
        status: "APPROVED",
        type: VacationType.ANNUAL,
        startDate: new Date("2026-07-01T00:00:00.000Z"),
        endDate: new Date("2026-07-02T00:00:00.000Z"),
        isHalfDay: false,
        resource: {
          id: "res_1",
          displayName: "Alice Example",
          eid: "EMP-001",
          chapter: "Delivery",
        },
        requestedBy: { id: "user_1", name: "Assistant User", email: "assistant@example.com" },
        effectiveDays: 2,
      }),
      update: vi.fn().mockImplementation(async (args?: any) => {
        const existing = await vacationFindUnique({ where: { id: args?.where?.id } });
        return {
          ...(existing ?? {
            id: args?.where?.id ?? "vac_unknown",
            resourceId: "res_1",
            startDate: new Date("2026-07-01T00:00:00.000Z"),
            endDate: new Date("2026-07-02T00:00:00.000Z"),
            type: VacationType.ANNUAL,
            isHalfDay: false,
          }),
          status: args?.data?.status ?? existing?.status ?? "APPROVED",
          rejectionReason: args?.data?.rejectionReason ?? null,
          approvedAt: args?.data?.approvedAt ?? null,
          approvedById: args?.data?.approvedById ?? existing?.approvedById ?? null,
        };
      }),
    },
    notification: {
      updateMany: vi.fn().mockResolvedValue({ count: 1 }),
      create: vi.fn().mockResolvedValue({ id: "note_1", userId: "user_1" }),
    },
    auditLog: {
      create: vi.fn().mockResolvedValue({ id: "audit_1" }),
    },
    webhook: {
      findMany: vi.fn().mockResolvedValue([]),
    },
  };
}

export const executeTool = executeAssistantTool;
