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

describe("updateSalesPriceRule", () => {
  it("updates mutable SalesPriceRule fields", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const updated = {
      ...draftPriceRule,
      unitPrice: 150,
      updatedAt: new Date("2024-01-02T00:00:00.000Z"),
    };
    mock.enqueueResults([draftPriceRule], [basePriceList], [updated]);

    const result = await mock.withTx((trx) =>
      run(trx, { salesPriceRuleId: draftPriceRule.id, unitPrice: 150, priority: 20 }, commandCtx),
    );

    expectOk(result);
  });

  it("returns error when SalesPriceRule does not exist", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

    const result = await mock.withTx((trx) =>
      run(trx, { salesPriceRuleId: draftPriceRule.id, unitPrice: 150 }, commandCtx),
    );

    expectErr(result, SalespriceruleNotFoundError);
  });

  it("returns error when SalesPriceRule is not editable", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([{ ...basePriceRule, status: "INACTIVE" }]);

    const result = await mock.withTx((trx) =>
      run(trx, { salesPriceRuleId: basePriceRule.id, unitPrice: 150 }, commandCtx),
    );

    expectErr(result, SalespriceruleNotEditableError);
  });

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

    const result = await mock.withTx((trx) =>
      run(trx, { salesPriceRuleId: draftPriceRule.id }, commandCtx),
    );

    expectErr(result, PriceListNotFoundError);
  });

  it("throws on invalid unit price", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([draftPriceRule], [basePriceList]);

    const result = await mock.withTx((trx) =>
      run(trx, { salesPriceRuleId: draftPriceRule.id, unitPrice: 0 }, commandCtx),
    );

    expectErr(result, InvalidUnitPriceError);
  });

  it("throws on invalid effective date range", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([draftPriceRule], [basePriceList]);

    const result = await mock.withTx((trx) =>
      run(
        trx,
        {
          salesPriceRuleId: draftPriceRule.id,
          effectiveFrom: new Date("2024-02-01T00:00:00.000Z"),
          effectiveTo: new Date("2024-01-01T00:00:00.000Z"),
        },
        commandCtx,
      ),
    );

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