import { baseApi } from "./baseApi";

export const collectionsApi = baseApi.injectEndpoints({
  endpoints: (builder) => ({
    getCollections: builder.query({
      query: () => `/collections`,
    }),
    
    getCollectionById: builder.query({
      query: (collectionId) => `/collections/${collectionId}`,
    }),
    
    getCollectionProducts: builder.query({
      query: (collectionId) => `/collections/${collectionId}/products`,
      transformResponse: (response) => {
        return {
          data: response.data || [],
          isEmpty: !response.data || response.data.length === 0,
        };
      },
    }),
    
    checkCollectionHasProducts: builder.query({
      query: (collectionId) => `/collections/${collectionId}/products?limit=1`,
      transformResponse: (response) => {
        return {
          hasProducts: response.data && response.data.length > 0,
        };
      },
    }),
    
    getCollectionProductsPaginated: builder.query({
      query: ({ collectionId, page = 1, limit = 6, brands, min_price, max_price }) => {
        const params = new URLSearchParams();
        params.append('page', page);
        if (limit) params.append('limit', limit);
        if (brands) params.append('brands', brands);
        if (min_price) params.append('min_price', min_price);
        if (max_price) params.append('max_price', max_price);
        
        return `/collections/${collectionId}/products?${params.toString()}`;
      },
      transformResponse: (response) => ({
        data: response.data || [],
        pagination: response.pagination || {},
        isEmpty: !response.data || response.data.length === 0,
      }),
    }),
  }),
});

export const {
  useGetCollectionsQuery,
  useGetCollectionByIdQuery,
  useGetCollectionProductsQuery,
  useCheckCollectionHasProductsQuery,
  useGetCollectionProductsPaginatedQuery,
  useLazyGetCollectionProductsPaginatedQuery,
  useLazyCheckCollectionHasProductsQuery,
} = collectionsApi;