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

describe("updateSalesPriceRule", () => {
  it("updates mutable SalesPriceRule fields", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const updated = {
      ...draftPriceRule,
      unitPrice: 150,
      updatedAt: new Date("2024-01-02T00:00:00.000Z"),
    };
    spies.select.mockReturnValueOnce(draftPriceRule).mockReturnValueOnce(basePriceList);
    spies.update.mockReturnValueOnce(updated);

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

    expectOk(result);
  });

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

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

    expectErr(result, SalespriceruleNotFoundError);
  });

  it("returns error when SalesPriceRule is not editable", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce({ ...basePriceRule, status: "INACTIVE" });

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

    expectErr(result, SalespriceruleNotEditableError);
  });

  it("returns error 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).mockReturnValueOnce(basePriceList);

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

    expectErr(result, InvalidUnitPriceError);
  });

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

    const result = await run(
      db,
      {
        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);
  });
});
