import api from "../axios";

export const company = {
  async fetch(
    accessToken: string,
    id: string,
    options?: {
      includeAddress?: boolean;
      includePrimaryContact?: boolean;
      includeAllContacts?: boolean;
      includeAllAddresses?: boolean;
    },
  ) {

    const params: Record<string, string> = {};
    if (options?.includeAddress) params.includeAddress = "true";
    if (options?.includePrimaryContact) params.includePrimaryContact = "true";
    if (options?.includeAllContacts) params.includeAllContacts = "true";
    if (options?.includeAllAddresses) params.includeAllAddresses = "true";


    const company = await api.get(`/v1/company/companies/${id}`, {
      params,
      headers: {
        Authorization: `Bearer ${accessToken}`,
      },
    });

    console.log(company.data);
    return company.data;
  },
  async fetchMany(
    accessToken: string,
    page: number = 1,
    search?: string,
    rpp: number = 30,
  ) {
    const params: Record<string, unknown> = { page, rpp };
    if (search && search.length > 0) params.search = search;

    const companies = await api.get("/v1/company/companies", {
      params,
      headers: {
        Authorization: `Bearer ${accessToken}`,
      },
    });

    return companies.data;
  },
  async count(accessToken: string) {
    const response = await api.get("/v1/company/count", {
      headers: {
        Authorization: `Bearer ${accessToken}`,
      },
    });
    return response.data.data.count;
  },
  async fetchConfigurations(accessToken: string, id: string) {
    const configurations = await api.get(
      `/v1/company/companies/${id}/configurations`,
      {
        headers: {
          Authorization: `Bearer ${accessToken}`,
        },
      },
    );
    return configurations.data;
  },
  async fetchSites(accessToken: string, id: string) {
    const response = await api.get(`/v1/company/companies/${id}/sites`, {
      headers: {
        Authorization: `Bearer ${accessToken}`,
      },
    });
    return response.data;
  },
};
