import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { baseActivePartner, baseDraftPartner, baseInactivePartner } from "../testing/fixtures";
import { run } from "./listPartnersByRole";

describe("listPartnersByRole", () => {
  it("returns all partners with CUSTOMER role", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseActivePartner, baseDraftPartner]);

    const result = await run(mock.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 mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseActivePartner]);

    const result = await run(mock.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 mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseActivePartner]);

    const result = await run(mock.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 mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseActivePartner, baseDraftPartner, baseInactivePartner]);

    const result = await run(mock.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 mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseActivePartner], [baseActivePartner]);

    const customerResult = await run(mock.db, { role: "CUSTOMER" });
    const supplierResult = await run(mock.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 mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

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

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