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

describe("activateSalesPriceRule", () => {
  it("activates a draft SalesPriceRule", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const activated = {
      ...draftPriceRule,
      status: "ACTIVE" as const,
      updatedAt: new Date("2024-01-02T00:00:00.000Z"),
    };
    spies.select.mockReturnValueOnce(draftPriceRule).mockReturnValueOnce(basePriceList);
    spies.update.mockReturnValueOnce(activated);

    const result = await run(db, { salesPriceRuleId: draftPriceRule.id }, commandCtx);

    expectOk(result);
  });

  it("throws when SalesPriceRule is not in DRAFT status", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(basePriceRule);

    const result = await run(db, { salesPriceRuleId: basePriceRule.id }, commandCtx);

    expectErr(result, InvalidSalespriceruleStatusError);
  });

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

    const result = await run(db, { salesPriceRuleId: draftPriceRule.id }, commandCtx);

    expectErr(result, SalespriceruleNotFoundError);
  });

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

    const result = await run(db, { salesPriceRuleId: draftPriceRule.id }, commandCtx);

    expectErr(result, PriceListNotFoundError);
  });

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

    const result = await run(db, { salesPriceRuleId: draftPriceRule.id }, commandCtx);

    expectErr(result, InvalidUnitPriceError);
  });

  it("throws on invalid effective date range", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select
      .mockReturnValueOnce({
        ...draftPriceRule,
        effectiveFrom: new Date("2024-02-01T00:00:00.000Z"),
        effectiveTo: new Date("2024-01-01T00:00:00.000Z"),
      })
      .mockReturnValueOnce(basePriceList);

    const result = await run(db, { salesPriceRuleId: draftPriceRule.id }, commandCtx);

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