import { describe, expect, it, vi } from "vitest";
import { SystemRole } from "@capakraken/shared";
import { rateCardRouter } from "../router/rate-card.js";
import { createCallerFactory } from "../trpc.js";

const createCaller = createCallerFactory(rateCardRouter);

function createControllerCaller(db: Record<string, unknown>) {
  return createCaller({
    session: {
      user: { email: "controller@example.com", name: "Controller", image: null },
      expires: "2099-01-01T00:00:00.000Z",
    },
    db: db as never,
    dbUser: {
      id: "user_1",
      systemRole: SystemRole.CONTROLLER,
      permissionOverrides: null,
    },
  });
}

function createManagerCaller(db: Record<string, unknown>) {
  return createCaller({
    session: {
      user: { email: "manager@example.com", name: "Manager", image: null },
      expires: "2099-01-01T00:00:00.000Z",
    },
    db: db as never,
    dbUser: {
      id: "user_2",
      systemRole: SystemRole.MANAGER,
      permissionOverrides: null,
    },
  });
}

describe("rateCard router", () => {
  describe("list", () => {
    it("returns rate cards with line counts through the router", async () => {
      const findMany = vi.fn().mockResolvedValue([
        { id: "rc_1", name: "Standard 2026", currency: "EUR", isActive: true, _count: { lines: 5 } },
        { id: "rc_2", name: "India Rates", currency: "INR", isActive: true, _count: { lines: 3 } },
      ]);

      const caller = createControllerCaller({
        rateCard: { findMany },
      });
      const result = await caller.list({
        search: "Standard",
        isActive: true,
      });

      expect(findMany).toHaveBeenCalledWith({
        where: {
          isActive: true,
          name: { contains: "Standard", mode: "insensitive" },
        },
        include: {
          _count: { select: { lines: true } },
          client: { select: { id: true, name: true, code: true } },
        },
        orderBy: [{ isActive: "desc" }, { effectiveFrom: "desc" }, { name: "asc" }],
      });
      expect(result).toHaveLength(2);
      expect(result[0]._count.lines).toBe(5);
    });
  });

  describe("create", () => {
    it("creates a rate card with lines through the router", async () => {
      const create = vi.fn().mockResolvedValue({
        id: "rc_new",
        name: "Q1 2026 Rates",
        currency: "EUR",
        isActive: true,
        lines: [
          { id: "rcl_1", costRateCents: 9500, billRateCents: 14000, chapter: "Digital Content Production" },
        ],
      });

      const caller = createManagerCaller({
        rateCard: { create },
      });
      const result = await caller.create({
        name: "Q1 2026 Rates",
        currency: "EUR",
        lines: [{ costRateCents: 9500, billRateCents: 14000, chapter: "Digital Content Production", attributes: {} }],
      });

      expect(create).toHaveBeenCalledWith(expect.objectContaining({
        data: expect.objectContaining({
          name: "Q1 2026 Rates",
          currency: "EUR",
        }),
      }));
      expect(result.id).toBe("rc_new");
      expect(result.lines).toHaveLength(1);
      expect(result.lines[0].costRateCents).toBe(9500);
    });
  });

  describe("resolveRate", () => {
    it("resolves a resource-based rate through the canonical router query", async () => {
      const db = {
        resource: {
          findUnique: vi.fn().mockResolvedValue({
            id: "res_1",
            displayName: "Bruce Banner",
            chapter: "Delivery",
            areaRole: { name: "Pipeline TD" },
          }),
        },
        role: {
          findFirst: vi.fn().mockResolvedValue({ id: "role_1" }),
        },
        rateCard: {
          findMany: vi.fn().mockResolvedValue([
            {
              id: "rc_2026",
              name: "Standard 2026",
              client: null,
              lines: [
                {
                  id: "line_1",
                  chapter: "Delivery",
                  seniority: "Senior",
                  costRateCents: 12_000,
                  billRateCents: 18_000,
                  role: { id: "role_1", name: "Pipeline TD" },
                },
              ],
            },
          ]),
        },
      };

      const caller = createControllerCaller(db);
      const result = await caller.resolveBestRate({ resourceId: "res_1" });

      expect(result).toEqual({
        rateCard: "Standard 2026",
        resource: "Bruce Banner",
        rate: "120,00 EUR",
        rateCents: 12000,
        matchedBy: "role: Pipeline TD",
      });
    });

    it("returns the most specific matching line", () => {
      const lines = [
        { id: "rcl_1", roleId: null, chapter: "Digital Content Production", costRateCents: 7000, billRateCents: 12000 },
        { id: "rcl_2", roleId: "role_3d", chapter: "Digital Content Production", costRateCents: 9500, billRateCents: 14000 },
        { id: "rcl_3", roleId: null, chapter: null, costRateCents: 6000, billRateCents: 10000 },
      ];

      const criteria = { roleId: "role_3d", chapter: "Digital Content Production" };

      const scored = lines.map((line) => {
        let score = 0;
        let mismatch = false;
        if (criteria.roleId && line.roleId) {
          if (line.roleId === criteria.roleId) score += 4;
          else mismatch = true;
        }
        if (criteria.chapter && line.chapter) {
          if (line.chapter === criteria.chapter) score += 2;
          else mismatch = true;
        }
        return { line, score, mismatch };
      });

      const candidates = scored
        .filter((s) => !s.mismatch)
        .sort((a, b) => b.score - a.score);

      const best = candidates[0];
      const result = best ? best.line : null;

      // Most specific match: role + chapter = score 6
      expect(result?.id).toBe("rcl_2");
      expect(result?.costRateCents).toBe(9500);
    });

    it("returns null when no lines match", () => {
      const lines = [
        { id: "rcl_1", roleId: "role_pm", chapter: "Project Management", costRateCents: 7000 },
      ];

      const criteria = { roleId: "role_3d", chapter: "Digital Content Production" };

      const scored = lines.map((line) => {
        let score = 0;
        let mismatch = false;
        if (criteria.roleId && line.roleId) {
          if (line.roleId === criteria.roleId) score += 4;
          else mismatch = true;
        }
        if (criteria.chapter && line.chapter) {
          if (line.chapter === criteria.chapter) score += 2;
          else mismatch = true;
        }
        return { line, score, mismatch };
      });

      const candidates = scored.filter((s) => !s.mismatch);
      const best = candidates[0];
      expect(best).toBeUndefined();
    });

    it("falls back to generic line when specific criteria don't match", () => {
      const lines = [
        { id: "rcl_1", roleId: null, chapter: null, costRateCents: 6000 },
        { id: "rcl_2", roleId: "role_pm", chapter: "Project Management", costRateCents: 8000 },
      ];

      const criteria = { roleId: "role_3d", chapter: "Digital Content Production" };

      const scored = lines.map((line) => {
        let score = 0;
        let mismatch = false;
        if (criteria.roleId && line.roleId) {
          if (line.roleId === criteria.roleId) score += 4;
          else mismatch = true;
        }
        if (criteria.chapter && line.chapter) {
          if (line.chapter === criteria.chapter) score += 2;
          else mismatch = true;
        }
        return { line, score, mismatch };
      });

      const candidates = scored
        .filter((s) => !s.mismatch)
        .sort((a, b) => b.score - a.score);

      const best = candidates[0];
      const result = best ? best.line : null;

      // Generic line (no criteria set) should match as fallback
      expect(result?.id).toBe("rcl_1");
      expect(result?.costRateCents).toBe(6000);
    });
  });

  describe("replaceLines", () => {
    it("deletes all lines and creates new ones in a transaction through the router", async () => {
      const deleteMany = vi.fn().mockResolvedValue({ count: 3 });
      const createLine = vi.fn()
        .mockResolvedValueOnce({ id: "rcl_new_1", costRateCents: 8000 })
        .mockResolvedValueOnce({ id: "rcl_new_2", costRateCents: 9500 });

      const caller = createManagerCaller({
        rateCard: {
          findUnique: vi.fn().mockResolvedValue({ id: "rc_1", name: "Standard 2026" }),
        },
        rateCardLine: {},
        $transaction: vi.fn(async (callback: (tx: { rateCardLine: { deleteMany: typeof deleteMany; create: typeof createLine } }) => unknown) => callback({
          rateCardLine: { deleteMany, create: createLine },
        })),
      });
      const result = await caller.replaceLines({
        rateCardId: "rc_1",
        lines: [
          { costRateCents: 8000, chapter: "Delivery", attributes: {} },
          { costRateCents: 9500, chapter: "Pipeline", attributes: {} },
        ],
      });

      expect(deleteMany).toHaveBeenCalledWith({ where: { rateCardId: "rc_1" } });
      expect(result).toEqual([
        { id: "rcl_new_1", costRateCents: 8000 },
        { id: "rcl_new_2", costRateCents: 9500 },
      ]);
    });
  });
});
