import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } 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 mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([activeDepartment, childDepartment]);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.items).toHaveLength(2);
    }
    expect(mock.selects).toHaveLength(1);
    expect(mock.selects[0].parameters).toContain("company-2");
    expect(mock.selects[0].sql).toMatch(/"companyId"/);
  });

  it("returns empty list when no departments exist", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

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

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.items).toHaveLength(1);
    }
    expect(mock.selects[0].parameters).toContain("ACTIVE");
    expect(mock.selects[0].sql).toMatch(/"status"/);
  });

  it("filters by parentDepartmentId when provided", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([childDepartment]);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.items).toHaveLength(1);
    }
    expect(mock.selects[0].parameters).toContain("dept-1");
    expect(mock.selects[0].sql).toMatch(/"parentDepartmentId"/);
  });

  it("filters root departments when parentDepartmentId is null", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([activeDepartment]);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.items).toHaveLength(1);
    }
    expect(mock.selects[0].sql).toMatch(/"parentDepartmentId" is null/i);
  });

  it("filters departments by INACTIVE status", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([inactiveDepartment]);

    const result = await run(mock.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");
    }
    expect(mock.selects[0].parameters).toContain("INACTIVE");
    expect(mock.selects[0].sql).toMatch(/"status"/);
  });

  it("returns departments ordered by name", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([activeDepartment, childDepartment]);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.items).toHaveLength(2);
    }
    expect(mock.selects[0].sql).toMatch(/order by .*"name" asc/i);
  });

  it("returns hasNextPage true when more results exist", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    // Simulate limit+1 strategy: 2 rows returned for limit=1 means hasNextPage=true
    mock.enqueueResults([activeDepartment, childDepartment]);

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

    const result = await run(mock.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);
    }
  });
});
