import { fetchBaserowRows } from "../lib/baserow";

export interface Client {
  name: string;
  logo: string;
}

interface BaserowClientRow {
  id: number;
  [key: string]: unknown;
  "Client Name"?: string;
  "URL Image"?: string;
  Active?: boolean;
}

export async function fetchClients(): Promise<Client[]> {
  const tableId = import.meta.env.BASEROW_CLIENT_TABLE_ID;

  if (!tableId) {
    console.warn(
      "BASEROW_TABLE_ID not set — returning empty client list",
    );
    return [];
  }

  try {
    const rows =
      await fetchBaserowRows<BaserowClientRow>(tableId);

    return rows
      .filter((row) => row["Client Name"] && row["URL Image"] && row.Active)
      .map((row) => ({
        name: row["Client Name"]!,
        logo: row["URL Image"]!,
      }));
  } catch (e) {
    console.error("Failed to fetch clients from Baserow:", e);
    return [];
  }
}
