import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { InvalidPricingContextError } from "../lib/errors.generated";
import { expectErr, expectOk } from "../testing/commandTestUtils";
import {
  baseCompany,
  baseItem,
  baseCurrency,
  baseUnit,
  basePriceList,
  basePriceRule,
  baseCustomer,
  baseChannel,
} from "../testing/fixtures";
import { run } from "./getApplicableSalesPrice";

describe("getApplicableSalesPrice", () => {
  it("returns the highest-precedence matching price rule", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    // First execute() → SalesPriceList, second execute() → SalesPriceRule
    mock.enqueueResults([basePriceList], [basePriceRule]);

    const result = await run(mock.db, {
      companyId: baseCompany.id,
      itemId: baseItem.id,
      customerId: baseCustomer.id,
      channelId: baseChannel.id,
      currencyId: baseCurrency.id,
      unitId: baseUnit.id,
      quantity: 5,
      pricingDate: new Date("2024-06-01T00:00:00.000Z"),
    });
    const { priceSuggestion } = expectOk<{
      priceSuggestion: { ruleId: string; unitPrice: number } | null;
    }>(result as never);

    expect(priceSuggestion).not.toBeNull();
    expect(priceSuggestion!.ruleId).toBe(basePriceRule.id);
    expect(priceSuggestion!.unitPrice).toBe(100);
  });

  it("returns unmatched result when no active rule applies", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([basePriceList], []);

    const result = await run(mock.db, {
      companyId: baseCompany.id,
      itemId: baseItem.id,
      customerId: baseCustomer.id,
      channelId: baseChannel.id,
      currencyId: baseCurrency.id,
      unitId: baseUnit.id,
      quantity: 5,
      pricingDate: new Date("2024-06-01T00:00:00.000Z"),
    });
    const { priceSuggestion } = expectOk<{
      priceSuggestion: { ruleId: string; unitPrice: number } | null;
    }>(result as never);

    expect(priceSuggestion).toBeNull();
  });

  it("resolves quantity-break pricing with compatible UoM conversion", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const higherBreakRule = {
      ...basePriceRule,
      id: "price-rule-high",
      minimumQuantity: 10,
      unitPrice: 80,
      priority: 20,
    };
    mock.enqueueResults([basePriceList], [basePriceRule, higherBreakRule]);

    const result = await run(mock.db, {
      companyId: baseCompany.id,
      itemId: baseItem.id,
      customerId: baseCustomer.id,
      channelId: baseChannel.id,
      currencyId: baseCurrency.id,
      unitId: baseUnit.id,
      quantity: 15,
      pricingDate: new Date("2024-06-01T00:00:00.000Z"),
    });
    const { priceSuggestion } = expectOk<{
      priceSuggestion: { ruleId: string; unitPrice: number } | null;
    }>(result as never);

    expect(priceSuggestion).not.toBeNull();
    expect(priceSuggestion!.ruleId).toBe("price-rule-high");
    expect(priceSuggestion!.unitPrice).toBe(80);
  });

  it("throws when required pricing context is missing", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();

    const result = await run(mock.db, {
      companyId: "",
      itemId: baseItem.id,
      currencyId: baseCurrency.id,
      unitId: baseUnit.id,
      quantity: 5,
    });

    expectErr(result as never, InvalidPricingContextError);
  });
});
