import api from "../axios";
import { API_ENDPOINTS } from "../endpoints";
import {
  ApiResponse,
  CompaniesListResponseSchema,
  TCompaniesResponse,
  TCompanyCategoryApiResponse,
  TCreateCompanyCategoryCredentials,
} from "../types";

import { ICreateCompanyCredentials } from "../types";

export const companiesService = {
  getCompanies: async (
    params?: Record<string, any>,
  ): Promise<ApiResponse<TCompaniesResponse>> => {
    try {
      const response = await api.get(API_ENDPOINTS.COMPANIES.READ_ALL, {
        params,
      });

      return CompaniesListResponseSchema.parse(response.data);
    } catch (error) {
      console.error("ERROR caught in Companies Services Read all:", error);
      throw error;
    }
  },

  createCompany: async (credentials: ICreateCompanyCredentials) => {
    const response = await api.post(
      API_ENDPOINTS.COMPANIES.CREATE,
      credentials,
    );
    return response.data;
  },
  updateCompany: async ({
    id,
    credentials,
  }: {
    id: string;
    credentials: ICreateCompanyCredentials;
  }) => {
    const response = await api.put(
      API_ENDPOINTS.COMPANIES.UPDATE(id),
      credentials,
    );
    return response.data;
  },

  companyDetails: async (id: string) => {
    const response = await api.get(API_ENDPOINTS.COMPANIES.DETAILS(id));
    return response.data;
  },
  deleteCompany: async (id: string) => {
    try {
      return (await api.delete(API_ENDPOINTS.COMPANIES.DELETE(id))).data;
    } catch (error) {
      console.error("ERROR caught in Companies Services Delete:", error);
      throw error;
    }
  },
};
export const companyCategoriesService = {
  getCategories: async (
    params?: Record<string, any>,
  ): Promise<TCompanyCategoryApiResponse> => {
    try {
      const response = await api.get(
        API_ENDPOINTS.COMPANIES.CATEGORIES.READ_ALL,
        {
          params,
        },
      );

      return response.data;
    } catch (error) {
      console.error("ERROR Caught in company categories service: ", error);
      throw error;
    }
  },

  getSingleCategory: async (id: string) => {
    try {
      return (await api.get(API_ENDPOINTS.COMPANIES.CATEGORIES.DETAILS(id)))
        .data;
    } catch (error) {
      console.error(
        "ERROR caught in Company categories service, Get single category",
        error,
      );
      throw error;
    }
  },
  createCategory: async (credentials: TCreateCompanyCategoryCredentials) => {
    try {
      const response = await api.post(
        API_ENDPOINTS.COMPANIES.CATEGORIES.CREATE,
        credentials,
      );
      return response.data;
    } catch (error) {
      console.error("ERROR Caught in company categories service: ", error);
      throw error;
    }
  },
  deleteCategory: async (id: string) => {
    try {
      return (await api.delete(API_ENDPOINTS.COMPANIES.CATEGORIES.DELETE(id)))
        .data;
    } catch (error) {
      console.error(
        "ERROR caught in Company Categories Services Delete:",
        error,
      );
      throw error;
    }
  },
  togglePublished: async (id: string) => {
    try {
      return (
        await api.patch(API_ENDPOINTS.COMPANIES.CATEGORIES.TOGGLE_PUBLISHED(id))
      ).data;
    } catch (error) {
      console.error(
        "ERROR caught in Company Categories Services Toggle Published:",
        error,
      );
      throw error;
    }
  },
   updateCategories: async ({
    id,
    credentials,
  }: {
    id: string;
    credentials: TCreateCompanyCategoryCredentials;
  }) => {
    const response = await api.put(
      API_ENDPOINTS.COMPANIES.CATEGORIES.UPDATE(id),
      credentials,
    );
    return response.data;
  },
};
