import CoCreationClient from "./CoCreationClient";
import { Metadata } from "next";

interface dhDTO {
  id: any;
  name: any;
  pieces_bio: any;
  logo: any;
  profile_images: any;
  based_in_city: any;
  bio_image: any;
  bio: any;
  herobgfrom: string;
  herolbgvia: string;
  herolbgto: string;
  pageBg: string;
  font_color: string;
  primary_color: string;
  secondary_color: string;
  hlgtColor: string;
  hexcode_for_bg: string;
  hexcode_for_button: string;
  hero_bg_from: string;
  hero_bg_to: string;
  hero_bg_via: string;
  high_light_color: string;
  product: any[];
  base64_id: string;
  slug: string;
}

export async function generateMetadata({
  searchParams,
}: {
  searchParams: Promise<Record<string, string>>;
}): Promise<Metadata> {
  const params = await searchParams;

  const category = params.category;

  return {
    alternates: {
      canonical: "https://mymorni.com/co-creation",
    },
    title: category
      ? "Co-Create Custom Clothing with Designers & Artisans | Morni"
      : "Shop Sustainable Clothing Collections | Morni",

    description: category
      ? "Collaborate with designers, stylists, and artisan-led design houses to create custom clothing made from natural fabrics. Bring your ideas to life through Morni's co-creation platform."
      : "Browse sustainable clothing collections by category, fabric, design house and style.",
  };
}

async function getDesignHouseProfile() {
  try {
    const response = await fetch(
      `${process.env.API_BASE_URL}/designHouseAndCoCreatorList`,
      {
        method: "GET",
        headers: { "Content-Type": "application/json" },
        next: { revalidate: 300 },
      },
    );

    if (!response.ok) {
      throw new Error(`Error: ${response.status} - ${response.statusText}`);
    }

    const data = await response.json();
    return {
      designHouse: data.data.design_house || [],
      coCreator: data.data.co_creators || [],
    };
  } catch (error) {
    console.error("Error fetching category list:", error);
    return {
      designHouse: [],
      coCreator: [],
    };
  }
}

export default async function CoCreation() {
  const { designHouse, coCreator } = await getDesignHouseProfile();

  return <CoCreationClient designHouse={designHouse} coCreator={coCreator} />;
}
