import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { Transaction } from "../generated/kysely-tailordb";
import {
  IdentifierRequiredError,
  InvalidEffectiveRangeError,
  InvalidUnitPriceError,
  PriceListNotFoundError,
} from "../lib/errors.generated";
import { basePriceList, basePriceRule } from "../testing/fixtures";
import { commandCtx, expectErr, expectOk } from "../testing/commandTestUtils";
import { run } from "./createSalesPriceRule";

describe("createSalesPriceRule", () => {
  it("creates a draft price rule with item, currency, and UoM context", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const created = { ...basePriceRule, id: "price-rule-9", status: "DRAFT" as const };
    spies.select.mockReturnValueOnce(basePriceList);
    spies.insert.mockReturnValueOnce(created);

    const result = await run(
      db,
      {
        salesPriceListId: basePriceList.id,
        itemId: basePriceRule.itemId,
        currencyId: basePriceRule.currencyId,
        unitId: basePriceRule.unitId,
        minimumQuantity: 1,
        unitPrice: 120,
        effectiveFrom: basePriceRule.effectiveFrom,
      },
      commandCtx,
    );

    const value = expectOk(result);
    expect(value.salesPriceRule).toEqual(created);
  });

  it("throws when required SalesPriceRule fields are missing", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(basePriceList);

    const result = await run(
      db,
      {
        salesPriceListId: basePriceList.id,
        itemId: "",
        currencyId: basePriceRule.currencyId,
        unitId: basePriceRule.unitId,
        unitPrice: 120,
      },
      commandCtx,
    );

    expectErr(result, IdentifierRequiredError);
  });

  it("creates the SalesPriceRule in DRAFT status", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(basePriceList);
    spies.insert.mockReturnValueOnce({ ...basePriceRule, status: "DRAFT" as const });

    const result = await run(
      db,
      {
        salesPriceListId: basePriceList.id,
        itemId: basePriceRule.itemId,
        currencyId: basePriceRule.currencyId,
        unitId: basePriceRule.unitId,
        unitPrice: 120,
      },
      commandCtx,
    );

    expectOk(result);
    expect(spies.values).toHaveBeenCalledWith(expect.objectContaining({ status: "DRAFT" }));
  });

  it("throws when the parent price list does not exist", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(undefined);

    const result = await run(
      db,
      {
        salesPriceListId: basePriceList.id,
        itemId: basePriceRule.itemId,
        currencyId: basePriceRule.currencyId,
        unitId: basePriceRule.unitId,
        unitPrice: 120,
      },
      commandCtx,
    );

    expectErr(result, PriceListNotFoundError);
  });

  it("throws on invalid unit price", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(basePriceList);

    const result = await run(
      db,
      {
        salesPriceListId: basePriceList.id,
        itemId: basePriceRule.itemId,
        currencyId: basePriceRule.currencyId,
        unitId: basePriceRule.unitId,
        unitPrice: 0,
      },
      commandCtx,
    );

    expectErr(result, InvalidUnitPriceError);
  });

  it("throws on invalid effective date range", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(basePriceList);

    const result = await run(
      db,
      {
        salesPriceListId: basePriceList.id,
        itemId: basePriceRule.itemId,
        currencyId: basePriceRule.currencyId,
        unitId: basePriceRule.unitId,
        unitPrice: 120,
        effectiveFrom: new Date("2024-02-01T00:00:00.000Z"),
        effectiveTo: new Date("2024-01-01T00:00:00.000Z"),
      },
      commandCtx,
    );

    expectErr(result, InvalidEffectiveRangeError);
  });
});
