import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { Transaction } from "../generated/kysely-tailordb";
import {
  CompanyNotFoundError,
  InvalidListPriorityError,
  PartnerNotSupplierError,
  SupplierNotActiveError,
  SupplierNotFoundError,
} from "../lib/errors.generated";
import { baseCompany, basePriceList, baseSupplier, inactiveSupplier } from "../testing/fixtures";
import { commandCtx, expectErr, expectOk } from "../testing/commandTestUtils";
import { run } from "./createPurchasePriceList";

describe("createPurchasePriceList", () => {
  it("creates an active price list for an active supplier", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select
      .mockReturnValueOnce(baseCompany)
      .mockReturnValueOnce(baseSupplier)
      .mockReturnValueOnce({ role: "SUPPLIER" });
    spies.insert.mockReturnValueOnce(basePriceList);

    const result = await run(
      db,
      {
        companyId: basePriceList.companyId,
        supplierId: basePriceList.supplierId,
        name: basePriceList.name,
        priority: basePriceList.priority,
      },
      commandCtx,
    );

    expect(expectOk(result).purchasePriceList.status).toBe("ACTIVE");
  });

  it("returns error when company does not exist", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(undefined);
    expectErr(
      await run(
        db,
        { companyId: "missing", supplierId: baseSupplier.id, name: "List", priority: 1 },
        commandCtx,
      ),
      CompanyNotFoundError,
    );
  });

  it("returns error when supplier does not exist", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseCompany).mockReturnValueOnce(undefined);
    expectErr(
      await run(
        db,
        { companyId: baseCompany.id, supplierId: "missing", name: "List", priority: 1 },
        commandCtx,
      ),
      SupplierNotFoundError,
    );
  });

  it("returns error when supplier is inactive", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseCompany).mockReturnValueOnce(inactiveSupplier);
    expectErr(
      await run(
        db,
        { companyId: baseCompany.id, supplierId: inactiveSupplier.id, name: "List", priority: 1 },
        commandCtx,
      ),
      SupplierNotActiveError,
    );
  });

  it("returns error when partner is not a supplier", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select
      .mockReturnValueOnce(baseCompany)
      .mockReturnValueOnce(baseSupplier)
      .mockReturnValueOnce(undefined);
    expectErr(
      await run(
        db,
        { companyId: baseCompany.id, supplierId: baseSupplier.id, name: "List", priority: 1 },
        commandCtx,
      ),
      PartnerNotSupplierError,
    );
  });

  it("returns error when list priority is invalid", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select
      .mockReturnValueOnce(baseCompany)
      .mockReturnValueOnce(baseSupplier)
      .mockReturnValueOnce({ role: "SUPPLIER" });
    expectErr(
      await run(
        db,
        { companyId: baseCompany.id, supplierId: baseSupplier.id, name: "List", priority: -1 },
        commandCtx,
      ),
      InvalidListPriorityError,
    );
  });
});
