import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { Transaction } from "../generated/kysely-tailordb";
import {
  EmptyPurchaseOrderLinesError,
  ItemNotActiveError,
  ItemNotFoundError,
  InvalidQuantityError,
  InvalidUnitPriceError,
  PartnerNotSupplierError,
  PaymentTermInactiveError,
  ReceivingSiteNotFoundError,
  ReceivingSiteRequiredError,
  SupplierNotActiveError,
  SupplierNotFoundError,
} from "../lib/errors.generated";
import {
  baseItem,
  basePaymentTerm,
  basePaymentTermLine,
  basePurchaseOrder,
  basePurchaseOrderLines,
  baseReceivingSite,
  baseSupplier,
  baseSupplierProfile,
  inactiveSupplier,
} from "../testing/fixtures";
import {
  commandCtx,
  expectErr,
  expectOk,
  mockBPQueries,
  mockItemQueries,
} from "../testing/commandTestUtils";
import { run } from "./createPurchaseOrder";

describe("createPurchaseOrder", () => {
  it("creates a draft purchase order with valid supplier and lines", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const bpQ = mockBPQueries({ partner: baseSupplier, partnerRole: { role: "SUPPLIER" } });
    const itemQ = mockItemQueries({ item: baseItem });
    spies.select.mockReturnValueOnce(undefined).mockReturnValueOnce(baseReceivingSite);
    spies.insert.mockReturnValueOnce(basePurchaseOrder);

    const result = await run(
      db,
      {
        companyId: basePurchaseOrder.companyId,
        supplierId: baseSupplier.id,
        lines: basePurchaseOrderLines,
      },
      commandCtx,
      bpQ,
      itemQ,
    );

    expect(expectOk(result).purchaseOrder.orderStatus).toBe("DRAFT");
  });

  it("inherits default payment term from supplier profile when present", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const bpQ = mockBPQueries({ partner: baseSupplier, partnerRole: { role: "SUPPLIER" } });
    const itemQ = mockItemQueries({ item: baseItem });
    spies.select
      .mockReturnValueOnce(baseSupplierProfile)
      .mockReturnValueOnce(basePaymentTerm)
      .mockReturnValueOnce([basePaymentTermLine])
      .mockReturnValueOnce(baseReceivingSite);
    spies.insert.mockReturnValueOnce(basePurchaseOrder);

    await run(
      db,
      {
        companyId: basePurchaseOrder.companyId,
        supplierId: baseSupplier.id,
        lines: basePurchaseOrderLines,
      },
      commandCtx,
      bpQ,
      itemQ,
    );

    expect(spies.values).toHaveBeenCalledWith(
      expect.objectContaining({ paymentTermId: basePaymentTerm.id }),
    );
  });

  it("returns error when supplier does not exist", async () => {
    const { db } = createMockDb<Transaction>();
    const bpQ = mockBPQueries({ partner: undefined });
    const itemQ = mockItemQueries();

    const result = await run(
      db,
      {
        companyId: basePurchaseOrder.companyId,
        supplierId: "missing",
        lines: basePurchaseOrderLines,
      },
      commandCtx,
      bpQ,
      itemQ,
    );

    expectErr(result, SupplierNotFoundError);
  });

  it("returns error when supplier is inactive", async () => {
    const { db } = createMockDb<Transaction>();
    const bpQ = mockBPQueries({ partner: inactiveSupplier });
    const itemQ = mockItemQueries();

    const result = await run(
      db,
      {
        companyId: basePurchaseOrder.companyId,
        supplierId: inactiveSupplier.id,
        lines: basePurchaseOrderLines,
      },
      commandCtx,
      bpQ,
      itemQ,
    );

    expectErr(result, SupplierNotActiveError);
  });

  it("returns error when partner is not a supplier", async () => {
    const { db } = createMockDb<Transaction>();
    const bpQ = mockBPQueries({ partner: baseSupplier, partnerRole: undefined });
    const itemQ = mockItemQueries();

    const result = await run(
      db,
      {
        companyId: basePurchaseOrder.companyId,
        supplierId: baseSupplier.id,
        lines: basePurchaseOrderLines,
      },
      commandCtx,
      bpQ,
      itemQ,
    );

    expectErr(result, PartnerNotSupplierError);
  });

  it("returns error when no lines are provided", async () => {
    const { db } = createMockDb<Transaction>();
    const bpQ = mockBPQueries({ partner: baseSupplier, partnerRole: { role: "SUPPLIER" } });
    const itemQ = mockItemQueries();

    const result = await run(
      db,
      { companyId: basePurchaseOrder.companyId, supplierId: baseSupplier.id, lines: [] },
      commandCtx,
      bpQ,
      itemQ,
    );

    expectErr(result, EmptyPurchaseOrderLinesError);
  });

  it("returns error when an item does not exist", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const bpQ = mockBPQueries({ partner: baseSupplier, partnerRole: { role: "SUPPLIER" } });
    const itemQ = mockItemQueries({ item: undefined });
    spies.select.mockReturnValueOnce(undefined);

    const result = await run(
      db,
      {
        companyId: basePurchaseOrder.companyId,
        supplierId: baseSupplier.id,
        lines: basePurchaseOrderLines,
      },
      commandCtx,
      bpQ,
      itemQ,
    );

    expectErr(result, ItemNotFoundError);
  });

  it("returns error when an item is inactive", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const bpQ = mockBPQueries({ partner: baseSupplier, partnerRole: { role: "SUPPLIER" } });
    const itemQ = mockItemQueries({ item: { ...baseItem, status: "INACTIVE" } });
    spies.select.mockReturnValueOnce(undefined);

    const result = await run(
      db,
      {
        companyId: basePurchaseOrder.companyId,
        supplierId: baseSupplier.id,
        lines: basePurchaseOrderLines,
      },
      commandCtx,
      bpQ,
      itemQ,
    );

    expectErr(result, ItemNotActiveError);
  });

  it("returns error when quantity is zero or negative", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const bpQ = mockBPQueries({ partner: baseSupplier, partnerRole: { role: "SUPPLIER" } });
    const itemQ = mockItemQueries({ item: baseItem });
    spies.select.mockReturnValueOnce(undefined);

    const result = await run(
      db,
      {
        companyId: basePurchaseOrder.companyId,
        supplierId: baseSupplier.id,
        lines: [{ ...basePurchaseOrderLines[0], quantity: "0" }],
      },
      commandCtx,
      bpQ,
      itemQ,
    );

    expectErr(result, InvalidQuantityError);
  });

  it("returns error when unit price is negative", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const bpQ = mockBPQueries({ partner: baseSupplier, partnerRole: { role: "SUPPLIER" } });
    const itemQ = mockItemQueries({ item: baseItem });
    spies.select.mockReturnValueOnce(undefined);

    const result = await run(
      db,
      {
        companyId: basePurchaseOrder.companyId,
        supplierId: baseSupplier.id,
        lines: [{ ...basePurchaseOrderLines[0], unitPrice: "-1" }],
      },
      commandCtx,
      bpQ,
      itemQ,
    );

    expectErr(result, InvalidUnitPriceError);
  });

  it("returns error when receiving site is missing for a physical line", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const bpQ = mockBPQueries({ partner: baseSupplier, partnerRole: { role: "SUPPLIER" } });
    const itemQ = mockItemQueries({ item: baseItem });
    spies.select.mockReturnValueOnce(undefined);

    const result = await run(
      db,
      {
        companyId: basePurchaseOrder.companyId,
        supplierId: baseSupplier.id,
        lines: [{ ...basePurchaseOrderLines[0], receivingSiteId: null }],
      },
      commandCtx,
      bpQ,
      itemQ,
    );

    expectErr(result, ReceivingSiteRequiredError);
  });

  it("returns error when receiving site does not exist", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const bpQ = mockBPQueries({ partner: baseSupplier, partnerRole: { role: "SUPPLIER" } });
    const itemQ = mockItemQueries({ item: baseItem });
    spies.select.mockReturnValueOnce(undefined).mockReturnValueOnce(undefined);

    const result = await run(
      db,
      {
        companyId: basePurchaseOrder.companyId,
        supplierId: baseSupplier.id,
        lines: basePurchaseOrderLines,
      },
      commandCtx,
      bpQ,
      itemQ,
    );

    expectErr(result, ReceivingSiteNotFoundError);
  });

  it("returns error when inherited payment term is inactive", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const bpQ = mockBPQueries({ partner: baseSupplier, partnerRole: { role: "SUPPLIER" } });
    const itemQ = mockItemQueries();
    spies.select
      .mockReturnValueOnce(baseSupplierProfile)
      .mockReturnValueOnce({ ...basePaymentTerm, status: "INACTIVE" });

    const result = await run(
      db,
      {
        companyId: basePurchaseOrder.companyId,
        supplierId: baseSupplier.id,
        lines: basePurchaseOrderLines,
      },
      commandCtx,
      bpQ,
      itemQ,
    );

    expectErr(result, PaymentTermInactiveError);
  });
});
