import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { DB } from "../generated/kysely-tailordb";
import { activeSite, inactiveSite } from "../testing/fixtures";
import { run } from "./listSitesByCompany";

describe("listSitesByCompany", () => {
  it("returns sites for a company", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([activeSite, inactiveSite]);

    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 sites 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([activeSite]);

    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 type when provided", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([inactiveSite]);

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

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

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

    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("combines status and type filters", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([inactiveSite]);

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

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

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

    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([activeSite, inactiveSite]);

    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([activeSite]);

    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);
    }
  });
});
