import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import {
  CompanyNotFoundError,
  InvalidListPriorityError,
  PartnerNotSupplierError,
  SupplierNotActiveError,
  SupplierNotFoundError,
} from "../lib/errors.generated";
import { commandCtx, expectErr, expectOk } from "../testing/commandTestUtils";
import {
  baseCompany,
  basePriceList,
  baseSupplier,
  baseSupplierRole,
  inactiveSupplier,
} from "../testing/fixtures";
import { mockBPQueries, mockOrgQueries } from "../testing/moduleMocks";
import { run } from "./createPurchasePriceList";

describe("createPurchasePriceList", () => {
  it("creates an active price list for an active supplier", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const bpQ = mockBPQueries({ partner: baseSupplier, partnerRole: baseSupplierRole });
    mock.enqueueResults([basePriceList]);

    const result = await mock.withTx((trx) =>
      run(
        trx,
        {
          companyId: basePriceList.companyId,
          supplierId: basePriceList.supplierId,
          name: basePriceList.name,
          priority: basePriceList.priority,
        },
        commandCtx,
        bpQ,
        mockOrgQueries({ company: baseCompany }),
      ),
    );

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

  it("returns error when company does not exist", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    expectErr(
      await mock.withTx((trx) =>
        run(
          trx,
          { companyId: "missing", supplierId: baseSupplier.id, name: "List", priority: 1 },
          commandCtx,
          mockBPQueries(),
          mockOrgQueries(),
        ),
      ),
      CompanyNotFoundError,
    );
  });

  it("returns error when supplier does not exist", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const bpQ = mockBPQueries();
    expectErr(
      await mock.withTx((trx) =>
        run(
          trx,
          { companyId: baseCompany.id, supplierId: "missing", name: "List", priority: 1 },
          commandCtx,
          bpQ,
          mockOrgQueries({ company: baseCompany }),
        ),
      ),
      SupplierNotFoundError,
    );
  });

  it("returns error when supplier is inactive", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const bpQ = mockBPQueries({ partner: inactiveSupplier });
    expectErr(
      await mock.withTx((trx) =>
        run(
          trx,
          { companyId: baseCompany.id, supplierId: inactiveSupplier.id, name: "List", priority: 1 },
          commandCtx,
          bpQ,
          mockOrgQueries({ company: baseCompany }),
        ),
      ),
      SupplierNotActiveError,
    );
  });

  it("returns error when partner is not a supplier", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const bpQ = mockBPQueries({ partner: baseSupplier });
    expectErr(
      await mock.withTx((trx) =>
        run(
          trx,
          { companyId: baseCompany.id, supplierId: baseSupplier.id, name: "List", priority: 1 },
          commandCtx,
          bpQ,
          mockOrgQueries({ company: baseCompany }),
        ),
      ),
      PartnerNotSupplierError,
    );
  });

  it("returns error when list priority is invalid", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const bpQ = mockBPQueries({ partner: baseSupplier, partnerRole: baseSupplierRole });
    expectErr(
      await mock.withTx((trx) =>
        run(
          trx,
          { companyId: baseCompany.id, supplierId: baseSupplier.id, name: "List", priority: -1 },
          commandCtx,
          bpQ,
          mockOrgQueries({ company: baseCompany }),
        ),
      ),
      InvalidListPriorityError,
    );
  });
});
