import type { ReadonlyDB } from "@tailor-platform/erp-kit/core";
import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { DB } from "../generated/kysely-tailordb";
import { baseRole, adminRole, inactiveRole } from "../testing/fixtures";
import { run } from "./listRoles";

describe("listRoles", () => {
  it("returns all roles when no filters applied", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseRole, adminRole, inactiveRole]);

    const result = await run(db as ReadonlyDB<DB>, {});

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.items).toHaveLength(3);
      expect(result.value.hasNextPage).toBe(false);
    }
  });

  it("returns roles filtered by ACTIVE status", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseRole, adminRole]);

    const result = await run(db as ReadonlyDB<DB>, { status: "ACTIVE" });

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

  it("returns roles filtered by INACTIVE status", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([inactiveRole]);

    const result = await run(db as ReadonlyDB<DB>, { status: "INACTIVE" });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.items).toHaveLength(1);
      expect(result.value.hasNextPage).toBe(false);
    }
  });

  it("returns empty list when no roles exist", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([]);

    const result = await run(db as ReadonlyDB<DB>, {});

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.items).toHaveLength(0);
      expect(result.value.hasNextPage).toBe(false);
    }
  });

  it("returns empty list when no roles match the status filter", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([]);

    const result = await run(db as ReadonlyDB<DB>, { status: "INACTIVE" });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.items).toHaveLength(0);
      expect(result.value.hasNextPage).toBe(false);
    }
  });
});
