import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { DB } from "../generated/kysely-tailordb";
import { activeDepartment, childDepartment, inactiveDepartment } from "../testing/fixtures";
import { run } from "./listDepartmentsByCompany";

describe("listDepartmentsByCompany", () => {
  it("returns departments for a company", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([activeDepartment, childDepartment]);

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

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

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

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

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

  it("filters by status when provided", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([activeDepartment]);

    const result = await run(db, { companyId: "company-2", status: "ACTIVE" });

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

  it("filters by parentDepartmentId when provided", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([childDepartment]);

    const result = await run(db, { companyId: "company-2", parentDepartmentId: "dept-1" });

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

  it("filters root departments when parentDepartmentId is null", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([activeDepartment]);

    const result = await run(db, { companyId: "company-2", parentDepartmentId: null });

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

  it("filters departments by INACTIVE status", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([inactiveDepartment]);

    const result = await run(db, { companyId: "company-2", status: "INACTIVE" });

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

  it("returns departments ordered by name", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([activeDepartment, childDepartment]);

    const result = await run(db, {
      companyId: "company-2",
      orderBy: "name",
      orderDirection: "asc",
    });

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

  it("returns hasNextPage true when more results exist", async () => {
    const { db, spies } = createMockDb<DB>();
    // Simulate limit+1 strategy: 2 rows returned for limit=1 means hasNextPage=true
    spies.select.mockReturnValue([activeDepartment, childDepartment]);

    const result = await run(db, { companyId: "company-2", limit: 1 });

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

  it("respects limit parameter", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([activeDepartment]);

    const result = await run(db, { companyId: "company-2", limit: 5 });

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