import Container from "@/components/elements/Container";
import { cookies } from "next/headers";
import InfinitePosts from "@/components/academy/InfinitePakage";
import { Metadata } from "next";
import AcademyFilter from "@/components/academy/AcademyFilter";
import { pageSeo } from "@/config/pageSeo";
import { generatePageMetadata } from "@/utils/generatePageMetadata";

interface IProjectsProps {
  params: Promise<{ slug?: string[] }>;
  searchParams: Promise<{
    search?: string;
    category?: string;
    minPrice?: string;
    maxPrice?: string;
    hasOffer?: string;
    type?: string;
    sortBy?: string;
    sortOrder?: string;
  }>;
}

export async function generateMetadata({
  searchParams,
}: IProjectsProps): Promise<Metadata> {
  const filters = await searchParams;

  const isDefaultAcademy =
    !filters.category &&
    !filters.type &&
    !filters.search &&
    !filters.minPrice &&
    !filters.maxPrice &&
    !filters.hasOffer &&
    !filters.sortBy;

  if (isDefaultAcademy) {
    return generatePageMetadata({
      title: pageSeo.academy.title,
      description: pageSeo.academy.description,
      path: pageSeo.academy.path,
      keywords: [...pageSeo.academy.keywords],
    });
  }

  const categoryLabel = filters.category || "حوزه زیبایی";
  const typeLabel = filters.type ? ` به صورت ${filters.type}` : "";
  const descTypeLabel = filters.type ? ` بصورت ${filters.type}` : "";

  const title = `آموزش ${categoryLabel}${typeLabel} | آموزشگاه مدستاگرام`;
  const description = `آموزش تخصصی و حرفه‌ای ${categoryLabel}${descTypeLabel} در آموزشگاه مدستاگرام. پکیج‌های آموزشی زیبایی، میکاپ، مدلینگ و عکاسی.`;

  const queryParams = new URLSearchParams();
  if (filters.category) queryParams.append("category", filters.category);
  if (filters.type) queryParams.append("type", filters.type);
  const queryString = queryParams.toString();
  const path = queryString
    ? `${pageSeo.academy.path}?${queryString}`
    : pageSeo.academy.path;

  return generatePageMetadata({ title, description, path });
}

export default async function Projects({
  searchParams,
}: {
  searchParams: Promise<{
    search?: string;
    category?: string;
    minPrice?: string;
    maxPrice?: string;
    hasOffer?: string;
    type?: string;
    sortBy?: string;
    sortOrder?: string;
  }>;
}) {
  const params = await searchParams;
  const cookieStore = await cookies();
  const token = cookieStore.get("token")?.value || "";

  const filters = {
    search: params.search || "",
    category: params.category || "",
    minPrice: params.minPrice ? Number(params.minPrice) : undefined,
    maxPrice: params.maxPrice ? Number(params.maxPrice) : undefined,
    hasOffer: params.hasOffer === "true",
    type: params.type || "",
    sortBy: params.sortBy || "createdAt",
    sortOrder: params.sortOrder || "desc",
  };

  return (
    <Container>
      <h1 className="sr-only">
        {pageSeo.academy.title.replace(" | مدستاگرام", "")}
      </h1>
      <AcademyFilter />
      <InfinitePosts filters={filters} token={token} />
    </Container>
  );
}
