import urlJoin from 'url-join';

import StructuredData from '@/components/StructuredData';
import { Locales } from '@/locales/resources';
import { ldModule } from '@/server/ld';
import { metadataModule } from '@/server/metadata';
import { DiscoverService } from '@/server/services/discover';
import { translation } from '@/server/translation';
import { AssistantCategory } from '@/types/discover';
import { isMobileDevice } from '@/utils/responsive';
import request from '@/app/api/request';
import List from '../features/List';

type Props = { params: { slug: AssistantCategory }; searchParams: { hl?: Locales } };

export const generateMetadata = async ({ params, searchParams }: Props) => {
  const { t, locale } = await translation('metadata', searchParams?.hl);
  const { t: td } = await translation('discover', searchParams?.hl);

  return metadataModule.generate({
    alternate: true,
    description: t('discover.assistants.description'),
    locale,
    title: [td(`category.assistant.${params.slug}`), t('discover.assistants.title')].join(' · '),
    url: urlJoin('/discover/assistants', params.slug),
  });
};

const Page = async ({ params, searchParams }: Props) => {
  const { t, locale } = await translation('metadata', searchParams?.hl);
  const { t: td } = await translation('discover', searchParams?.hl);
  const mobile = isMobileDevice();
  const discoverService = new DiscoverService();
  console.log(params.slug,3333333)
  let items;
  if(params.slug == "collect") {
    const res = await request({
        url: "/flxai/api/robot/appaiassistant/getAllAiAssistant",
        method: "get",
        params: {
          userid: searchParams.userid
        }
    })
    items = res.data;
    items = items.map((item,index)=> {
        item.classify = 'collect';
        return item
    })
  } else {
    const res = await request({
        url: "/flxai/api/robot/appaiassistant/getAllAiAssistant",
        method: "get",
        params: {
          userid: searchParams.userid
        }
    })
    const array2Object = {};
    res?.data?.forEach(item => {
      array2Object[item.identifier] = item;
    });
    items = await discoverService.getAssistantCategory(locale, params.slug);
    items = items.map((item,index)=> {
        const matchingItem = array2Object[item.identifier];
        if (matchingItem) {
          console.log('3838383838',matchingItem);
          matchingItem.status = '1';
          return matchingItem
        } else {
          item.status = '0';
          return item
        }
    })
  }
  console.log(8272772727,items)
  const ld = ldModule.generate({
    description: t('discover.assistants.description'),
    title: [td(`category.assistant.${params.slug}`), t('discover.assistants.title')].join(' · '),
    url: urlJoin('/discover/assistants', params.slug + '?userid=' + searchParams.userid),
    webpage: {
      enable: true,
      search: '/discover/search',
    },
  });

  return (
    <>
      <StructuredData ld={ld} />
      <List category={params.slug} items={items} mobile={mobile} />
    </>
  );
};

export const generateStaticParams = async () => {
  const cates = Object.values(AssistantCategory);
  return cates.map((cate) => ({
    slug: cate,
  }));
};

Page.DisplayName = 'DiscoverAssistantsCategory';

export default Page;
