import { useCategoryStore } from "@/zustand/categoryStore";
import apiClient from "../api-client";
import {
  AllCategoryResponse,
  CategoryBySlugResponse,
  CollectionBySlugResponse,
} from "../types/models/category";
import {
  IndustriesProductResponse,
  ProductResponse,
} from "../types/models/product";
export default class categoryService {
  static fetchCategories = async (): Promise<AllCategoryResponse> => {
    try {
      const store = useCategoryStore.getState();
      const response = await apiClient.get<AllCategoryResponse>(
        `/api/categories`
      );
      const resources: AllCategoryResponse = response.data;
      store.setCategories(resources.data || []);
      return response.data || [];
    } catch (error) {
      console.error(`Error in categoryService:fetchCategories`, error);
      throw error;
    }
  };

  static fetchCategoryBySlug = async (
    id: string
  ): Promise<CategoryBySlugResponse> => {
    try {
      const response = await apiClient.get<CategoryBySlugResponse>(
        `/api/categories/${id}`
      );
      return response.data;
    } catch (error) {
      console.error(`Error in categoryService:fetchCategoryBySlug`, error);
      throw error;
    }
  };

  static fetchCollectionBySlug = async (
    id: string
  ): Promise<CollectionBySlugResponse> => {
    try {
      const response = await apiClient.get<CollectionBySlugResponse>(
        `/api/collections/${id}`
      );
      return response.data;
    } catch (error) {
      console.error(`Error in categoryService:fetchCollectionBySlug`, error);
      throw error;
    }
  };

  static fetchProductByCategory = async (
    payload: string
  ): Promise<ProductResponse> => {
    try {
      const response = await apiClient.get<ProductResponse>(
        `/api/products?${payload}`
      );
      return response.data;
    } catch (error) {
      console.error(`Error in categoryService:fetchProductByCategory`, error);
      throw error;
    }
  };

  static fetchIndustriesProduct = async (payload: {
    limit: 15 | 50 | 100;
    page: number;
    ids: string[];
  }): Promise<IndustriesProductResponse> => {
    try {
      const response = await apiClient.post<IndustriesProductResponse>(
        `/v1/api/categories/industries-list`,
        payload
      );
      return response.data;
    } catch (error) {
      console.error(`Error in categoryService:fetchIndustriesProduct`, error);
      throw error;
    }
  };
}
