import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { DB } from "../generated/kysely-tailordb";
import {
  CostCenterNotFoundError,
  InvalidOrderTypeFilterError,
  InvalidStatusFilterError,
} from "../lib/errors.generated";
import { baseInternalOrder, releasedInternalOrder, baseCostCenter } from "../testing/fixtures";
import { run } from "./listInternalOrders";

describe("listInternalOrders", () => {
  it("returns all internal orders when no filters are provided", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseInternalOrder, releasedInternalOrder]);

    const result = await run(db, {});

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.items).toHaveLength(2);
    }
  });

  it("returns internal orders filtered by companyId", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseInternalOrder]);

    const result = await run(db, { companyId: baseInternalOrder.companyId });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.items).toHaveLength(1);
      expect(result.value.items[0]).toEqual(baseInternalOrder);
    }
  });

  it("returns internal orders filtered by orderType OVERHEAD", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseInternalOrder]);

    const result = await run(db, { orderType: "OVERHEAD" });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.items).toHaveLength(1);
      expect(result.value.items[0].orderType).toEqual("OVERHEAD");
    }
  });

  it("returns internal orders filtered by status RELEASED", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([releasedInternalOrder]);

    const result = await run(db, { status: "RELEASED" });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.items).toHaveLength(1);
      expect(result.value.items[0].status).toEqual("RELEASED");
    }
  });

  it("returns internal orders filtered by costCenterId", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select
      .mockReturnValueOnce({ id: baseCostCenter.id })
      .mockReturnValueOnce([baseInternalOrder]);

    const result = await run(db, { costCenterId: baseCostCenter.id });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.items).toHaveLength(1);
      expect(result.value.items[0].costCenterId).toEqual(baseCostCenter.id);
    }
  });

  it("returns internal orders matching multiple filters simultaneously", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([releasedInternalOrder]);

    const result = await run(db, {
      companyId: releasedInternalOrder.companyId,
      orderType: "OVERHEAD",
      status: "RELEASED",
    });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.items).toHaveLength(1);
      expect(result.value.items[0]).toEqual(releasedInternalOrder);
    }
  });

  it("returns an empty list when no internal orders match the given filters", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([]);

    const result = await run(db, { companyId: "non-existent" });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.items).toEqual([]);
    }
  });

  it("returns error when orderType filter is invalid", async () => {
    const { db } = createMockDb<DB>();
    const result = await run(db, { orderType: "INVALID" });
    expect(result.ok).toBe(false);
    if (!result.ok) expect(result.error).toBeInstanceOf(InvalidOrderTypeFilterError);
  });

  it("returns error when status filter is invalid", async () => {
    const { db } = createMockDb<DB>();
    const result = await run(db, { status: "INVALID" });
    expect(result.ok).toBe(false);
    if (!result.ok) expect(result.error).toBeInstanceOf(InvalidStatusFilterError);
  });

  it("returns error when costCenterId does not exist", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce(undefined);
    const result = await run(db, { costCenterId: "missing-cost-center" });
    expect(result.ok).toBe(false);
    if (!result.ok) expect(result.error).toBeInstanceOf(CostCenterNotFoundError);
  });
});
