import type { Client } from "@fattureincloud/fattureincloud-ts-sdk";
import { clientsApi, companyId } from "~/server/FattureInCloud";

export const getClientList = async () => {
  const clients = await clientsApi.listClients(
    companyId,
    undefined,
    "detailed",
    undefined,
    undefined,
    100,
  );
  return clients.data;
};

export const searchClient = async (cognome: string) => {
  const clients = await clientsApi.listClients(
    companyId,
    undefined,
    "detailed",
    undefined,
    undefined,
    100,
    `name contains '${cognome}'`,
  );
  return clients.data;
};
export const getClientFromCF = async (cf: string) => {
  if (cf.length < 11) {
    return null;
  }
  const clients = await clientsApi.listClients(
    companyId,
    undefined,
    "detailed",
    undefined,
    undefined,
    100,
    `tax_code = '${cf}'`,
  );
  return clients.data;
};

export const addClient = async (client: Client) => {
  const newClient = await clientsApi.createClient(companyId, { data: client });
  return newClient.data;
};
