import Container from "@/components/elements/Container";
import ModelsFilter from "@/components/models/ModelsFilter";
import { cookies } from "next/headers";
import InfinitePosts from "@/components/models/InfinitePosts";
import StoriesBar from "@/components/stories/StoriesBar";
import Header from "@/components/main/Header";
import TabNavigation from "@/components/TabNavigation";
import LocalePageShell from "@/components/i18n/LocalePageShell";
import { Metadata } from "next";
import { getSeoPage } from "@/lib/i18n/seo";
import { getServerLanguage } from "@/lib/i18n/server";
import { generatePageMetadata } from "@/utils/generatePageMetadata";
import {
  buildHomeListSeo,
  resolveHomeListFilters,
} from "@/lib/buildHomeListSeo";
import { normalizeUserLevel } from "@/lib/userLevel";

interface IModelsProps {
  params: Promise<{ slug?: string[] }>;
  searchParams: Promise<{
    expertise?: string;
    province?: string;
    city?: string;
    userLevel?: string;
    rateFilter?: string;
    hashtag?: string;
    heightMin?: string;
    heightMax?: string;
    weightMin?: string;
    weightMax?: string;
    sizeMin?: string;
    sizeMax?: string;
    hair_color?: string;
    eye_color?: string;
  }>;
}

export async function generateMetadata({ params, searchParams }: IModelsProps): Promise<Metadata> {
  const lang = await getServerLanguage();
  const [urlParams, filters] = await Promise.all([params, searchParams]);
  const slugPath =
    urlParams.slug && urlParams.slug.length > 0
      ? `/${urlParams.slug.join("/")}`
      : undefined;

  const seoFilters = resolveHomeListFilters(urlParams.slug || [], filters);
  const seo = buildHomeListSeo(seoFilters, slugPath, lang);
  const homePage = getSeoPage("home", lang);

  const meta = generatePageMetadata({
    title: seo.title,
    ogTitle: seo.title,
    description: seo.description,
    path: seo.path,
    keywords: homePage.keywords,
    lang,
  });

  return {
    ...meta,
    title: { absolute: seo.title },
  };
}

export default async function Models({ params, searchParams }: IModelsProps) {
  const [urlParams, filters, cookieStore] = await Promise.all([
    params,
    searchParams,
    cookies()
  ]);
  
  const token = cookieStore.get("token")?.value || "";
  const seoFilters = resolveHomeListFilters(urlParams.slug || [], filters);
  const seo = buildHomeListSeo(
    seoFilters,
    urlParams.slug?.length ? `/${urlParams.slug.join("/")}` : undefined
  );

  let provinceId = seoFilters.province || "";
  let cityId = seoFilters.city || "";
  let userLevel = normalizeUserLevel(seoFilters.userLevel || "");
  let expertise = seoFilters.expertise || "";

  return (
    <>
      <Header />
      <LocalePageShell>
      <Container>
        <h1 className="sr-only">{seo.h1}</h1>
        
        <ModelsFilter expertise={expertise || filters.expertise || ""} />

        <StoriesBar token={token} />

        <InfinitePosts 
          filters={{ 
            ...filters, 
            hashtag: filters.hashtag,
            province: provinceId, 
            city: cityId, 
            userLevel: userLevel || normalizeUserLevel(filters.userLevel),
            expertise: expertise || filters.expertise,
            heightMin: filters.heightMin,
            heightMax: filters.heightMax,
            weightMin: filters.weightMin,
            weightMax: filters.weightMax,
            sizeMin: filters.sizeMin,
            sizeMax: filters.sizeMax,
            hair_color: filters.hair_color,
            eye_color: filters.eye_color,
          }} 
          token={token} 
        />
      </Container>
      </LocalePageShell>
      <TabNavigation currentPage="/" />
    </>
  );
}
