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

describe("activateSalesPriceRule", () => {
  it("activates a draft SalesPriceRule", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const activated = {
      ...draftPriceRule,
      status: "ACTIVE" as const,
      updatedAt: new Date("2024-01-02T00:00:00.000Z"),
    };
    mock.enqueueResults([draftPriceRule], [basePriceList], [activated]);

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

    expectOk(result);
  });

  it("throws when SalesPriceRule is not in DRAFT status", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([basePriceRule]);

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

    expectErr(result, InvalidSalespriceruleStatusError);
  });

  it("throws 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 }, commandCtx),
    );

    expectErr(result, SalespriceruleNotFoundError);
  });

  it("throws 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, unitPrice: 0 }], [basePriceList]);

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

    expectErr(result, InvalidUnitPriceError);
  });

  it("throws on invalid effective date range", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults(
      [
        {
          ...draftPriceRule,
          effectiveFrom: new Date("2024-02-01T00:00:00.000Z"),
          effectiveTo: new Date("2024-01-01T00:00:00.000Z"),
        },
      ],
      [basePriceList],
    );

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

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