import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { activeSite, inactiveSite } from "../testing/fixtures";
import { run } from "./listSitesByCompany";

describe("listSitesByCompany", () => {
  it("returns sites for a company", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([activeSite, inactiveSite]);

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

    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 type when provided", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([inactiveSite]);

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

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

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

    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("combines status and type filters", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([inactiveSite]);

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

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

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

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

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