import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { DB } from "../generated/kysely-tailordb";
import { InvalidRoleError } from "../lib/errors.generated";
import { baseActivePartner, baseDraftPartner, baseInactivePartner } from "../testing/fixtures";
import { run } from "./listPartnersByRole";

describe("listPartnersByRole", () => {
  it("returns all partners with CUSTOMER role", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([baseActivePartner, baseDraftPartner]);

    const result = await run(db, { role: "CUSTOMER" });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.items).toEqual([baseActivePartner, baseDraftPartner]);
      expect(result.value.hasNextPage).toBe(false);
    }
  });

  it("returns all partners with SUPPLIER role", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([baseActivePartner]);

    const result = await run(db, { role: "SUPPLIER" });

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

  it("returns only ACTIVE partners when status filter is ACTIVE", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([baseActivePartner]);

    const result = await run(db, { role: "CUSTOMER", status: "ACTIVE" });

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

  it("returns partners of all statuses when status filter is omitted", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([baseActivePartner, baseDraftPartner, baseInactivePartner]);

    const result = await run(db, { role: "CUSTOMER" });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.items).toEqual([
        baseActivePartner,
        baseDraftPartner,
        baseInactivePartner,
      ]);
      expect(result.value.hasNextPage).toBe(false);
    }
  });

  it("returns partner in both queries when partner holds both roles", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([baseActivePartner]);

    const customerResult = await run(db, { role: "CUSTOMER" });
    const supplierResult = await run(db, { role: "SUPPLIER" });

    expect(customerResult.ok).toBe(true);
    expect(supplierResult.ok).toBe(true);
    if (customerResult.ok && supplierResult.ok) {
      expect(customerResult.value.items).toEqual([baseActivePartner]);
      expect(supplierResult.value.items).toEqual([baseActivePartner]);
    }
  });

  it("returns empty array when no partners match the criteria", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([]);

    const result = await run(db, { role: "SUPPLIER" });

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

  it("returns error for invalid role value", async () => {
    const { db } = createMockDb<DB>();

    const result = await run(db, { role: "INVALID_ROLE" });

    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(InvalidRoleError);
    }
  });
});
