import { describe, expect, it } from "vitest";
import {
  buildRateCardCreateAuditAfter,
  buildRateCardCreateData,
  buildRateCardLineCreateAuditAfter,
  buildRateCardLineCreateData,
  buildRateCardLineUpdateData,
  buildRateCardListWhere,
  buildRateCardReplaceLinesAuditAfter,
  buildRateCardUpdateData,
  rateCardCreateInclude,
  rateCardDetailInclude,
  rateCardLineOrderBy,
  rateCardSummaryInclude,
} from "../router/rate-card-write-support.js";

describe("rate card write support", () => {
  it("exposes shared include definitions", () => {
    expect(rateCardSummaryInclude).toEqual({
      _count: { select: { lines: true } },
      client: { select: { id: true, name: true, code: true } },
    });

    expect(rateCardLineOrderBy).toEqual([
      { chapter: "asc" },
      { seniority: "asc" },
      { createdAt: "asc" },
    ]);

    expect(rateCardDetailInclude).toEqual({
      client: { select: { id: true, name: true, code: true } },
      lines: {
        select: {
          id: true,
          rateCardId: true,
          roleId: true,
          chapter: true,
          seniority: true,
          location: true,
          workType: true,
          serviceGroup: true,
          costRateCents: true,
          billRateCents: true,
          machineRateCents: true,
          attributes: true,
          role: { select: { id: true, name: true } },
        },
        orderBy: rateCardLineOrderBy,
      },
    });

    expect(rateCardCreateInclude).toEqual({
      lines: {
        select: rateCardDetailInclude.lines.select,
      },
    });
  });

  it("builds rate card list filters including effective date windows", () => {
    expect(buildRateCardListWhere({
      isActive: true,
      clientId: "client_1",
      search: "Standard",
      effectiveAt: new Date("2026-05-15T00:00:00.000Z"),
    })).toEqual({
      isActive: true,
      clientId: "client_1",
      name: { contains: "Standard", mode: "insensitive" },
      OR: [
        { effectiveFrom: null },
        { effectiveFrom: { lte: new Date("2026-05-15T00:00:00.000Z") } },
      ],
      AND: [
        {
          OR: [
            { effectiveTo: null },
            { effectiveTo: { gte: new Date("2026-05-15T00:00:00.000Z") } },
          ],
        },
      ],
    });
  });

  it("builds nested create data for a rate card and its lines", () => {
    expect(buildRateCardCreateData({
      name: "Q1 2026",
      currency: "EUR",
      effectiveFrom: new Date("2026-01-01T00:00:00.000Z"),
      clientId: "client_1",
      lines: [{
        roleId: "role_1",
        chapter: "Delivery",
        costRateCents: 9500,
        billRateCents: 14000,
        attributes: { level: "senior" },
      }],
    })).toEqual({
      name: "Q1 2026",
      currency: "EUR",
      effectiveFrom: new Date("2026-01-01T00:00:00.000Z"),
      clientId: "client_1",
      lines: {
        create: [{
          roleId: "role_1",
          chapter: "Delivery",
          costRateCents: 9500,
          billRateCents: 14000,
          attributes: { level: "senior" },
        }],
      },
    });
  });

  it("builds standalone line create data with rate card id", () => {
    expect(buildRateCardLineCreateData("rc_1", {
      chapter: "Delivery",
      costRateCents: 8000,
      attributes: {},
    })).toEqual({
      rateCardId: "rc_1",
      chapter: "Delivery",
      costRateCents: 8000,
      attributes: {},
    });
  });

  it("builds sparse rate card updates", () => {
    expect(buildRateCardUpdateData({
      name: "Updated 2026",
      isActive: false,
    })).toEqual({
      name: "Updated 2026",
      isActive: false,
    });
  });

  it("builds line updates including role disconnect semantics", () => {
    expect(buildRateCardLineUpdateData({
      roleId: null,
      costRateCents: 12345,
      attributes: { region: "de" },
    })).toEqual({
      role: { disconnect: true },
      costRateCents: 12345,
      attributes: { region: "de" },
    });
  });

  it("builds compact audit payloads", () => {
    expect(buildRateCardCreateAuditAfter({
      name: "Q1 2026",
      currency: "EUR",
      lines: [
        { costRateCents: 9500, attributes: {} },
        { costRateCents: 8000, attributes: {} },
      ],
    })).toEqual({
      name: "Q1 2026",
      currency: "EUR",
      lineCount: 2,
    });

    expect(buildRateCardLineCreateAuditAfter({
      rateCardId: "rc_1",
      line: {
        chapter: "Delivery",
        costRateCents: 9500,
        billRateCents: 14000,
      },
    })).toEqual({
      rateCardId: "rc_1",
      chapter: "Delivery",
      costRateCents: 9500,
      billRateCents: 14000,
    });

    expect(buildRateCardReplaceLinesAuditAfter(3)).toEqual({
      replacedLineCount: 3,
    });
  });
});
