import odooApi from '@/core/api/odooApi'
import {React } from 'react'
import axios from 'axios';
import { useQuery } from 'react-query'
import Link from '@/core/components/elements/Link/Link'
import { createSlug } from '@/core/utils/slug'
import Image from 'next/image'
import { ChevronRightIcon } from '@heroicons/react/24/outline'
import useProductSearch from '../../../lib/product/hooks/useProductSearch';
import { SolrResponse } from "~/types/solr";
import {fetchPromoItemsSolr} from '../api/popularProduct'
const SOLR_HOST = process.env.SOLR_HOST

const PopularBrand = ({ category }) => {

    const queryFinal = {
        fq: `category_id_ids:(${category.categoryDataIds.join(' OR ')})`,
        fl: 'manufacture_name_s,qty_sold_f',
        sort: 'qty_sold_f desc',
        rows: '2000'
    };
    
    // Konversi objek queryFinal menjadi string query
    // const queryString = new URLSearchParams(queryFinal).toString();
    
    async function fetchTopBrands() {
        try {
            // const items = await fetchPromoItemsSolr(`category_id_ids:(${category.categoryDataIds.join(' OR ')})`); 
            const items = await fetchPromoItemsSolr(`type_value_s:discount_loading`);

            console.log("queryFinal", queryFinal);
            console.log("items", items);
    
            // Fungsi untuk deduplikasi dan mengambil 12 nama brand teratas
            function getTop12UniqueBrands(products) {
                const brandSet = new Set();
                const topBrands = [];
    
                for (const product of products) {
                    if (!brandSet.has(product.manufacture_name_s)) {
                        brandSet.add(product.manufacture_name_s);
                        topBrands.push(product.manufacture_name_s);
                    }
                    if (topBrands.length === 12) break;
                }
    
                return topBrands;
            }
    
            // Menggunakan hasil pencarian produk
            const products = items;
            const top12UniqueBrands = getTop12UniqueBrands(products);
    
            console.log('top12UniqueBrands',top12UniqueBrands);
            return top12UniqueBrands;
        } catch (error) {
            console.error("Error fetching data from Solr", error);
            throw error;
        }
    }
    
    fetchTopBrands();


  return (
    <div className='flex flex-col'>
                  <div className='grid grid-cols-2 max-h-full w-full gap-2'>
                    {category.childs.map((brand, index) => (
                      (index < 8 ) && (
                        <div key={brand.id} className='w-full flex items-center justify-center pb-2'>
                          <Link
                            href={createSlug('/shop/category/', brand.name, brand.id)}
                            className='category-mega-box__child-one w-fit h-full flex items-center justify-center '
                          >
                            <Image src='https://erp.indoteknik.com/api/image/x_manufactures/x_logo_manufacture/661' alt='' width={104} height={44} objectFit='cover' />
                          </Link>
                        </div>
                      )
                    ))}
                  </div>
                  {category.childs.length > 8 && (
                    <div className='flex hover:bg-gray_r-8/35 rounded-10'>
                      <Link
                        href={createSlug('/shop/category/', category.name, category.id)}
                        className='category-mega-box__child-one flex items-center gap-4 font-bold hover:ml-4'
                      >
                        <p className='mt-2 mb-0 text-danger-500 font-semibold'>Lihat Semua Brand</p>
                        <ChevronRightIcon className='w-4 text-danger-500 font-bold' />
                      </Link>
                    </div>
        )}
    </div>
  )
}

export default PopularBrand
