import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import {
  IdentifierRequiredError,
  InvalidEffectiveRangeError,
  InvalidUnitPriceError,
  PriceListNotFoundError,
} from "../lib/errors.generated";
import { commandCtx, expectErr, expectOk } from "../testing/commandTestUtils";
import { basePriceList, basePriceRule } from "../testing/fixtures";
import { run } from "./createSalesPriceRule";

describe("createSalesPriceRule", () => {
  it("creates a draft price rule with item, currency, and UoM context", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const created = { ...basePriceRule, id: "price-rule-9", status: "DRAFT" as const };
    mock.enqueueResults([basePriceList], [created]);

    const result = await mock.withTx((trx) =>
      run(
        trx,
        {
          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 mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([basePriceList]);

    const result = await mock.withTx((trx) =>
      run(
        trx,
        {
          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 mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([basePriceList], [{ ...basePriceRule, status: "DRAFT" as const }]);

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

    expectOk(result);
    expect(mock.inserts[0].insertValues()).toEqual(expect.objectContaining({ status: "DRAFT" }));
  });

  it("throws when the parent price list does not exist", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

    const result = await mock.withTx((trx) =>
      run(
        trx,
        {
          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 mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([basePriceList]);

    const result = await mock.withTx((trx) =>
      run(
        trx,
        {
          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 mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([basePriceList]);

    const result = await mock.withTx((trx) =>
      run(
        trx,
        {
          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);
  });
});
