import PreferenceWrapper from '@/components/wrapper/PreferenceWrapper'
import React from 'react'
import type { Metadata, Viewport } from 'next'
import { getShop } from './lib/getShop'
import { notFound } from 'next/navigation'
import CartChecker from '@/components/CartChecker'
import ActiveChecker from '@/components/ActiveChecker'

export const dynamic = 'force-dynamic'
export const revalidate = 0

type LayoutParams = { name: string }

export async function generateMetadata({
  params
}: {
  params: Promise<LayoutParams>
}): Promise<Metadata> {
  const { name } = await params
  try {
    const shop = await getShop(name)

    const title = shop.seoTitle || shop.name
    const logo = shop.logo

    const defaultIcon = "/icons/web-app-manifest-192x192.png"

    // استفاده از API route برای تبدیل لوگو به مربع
    let iconUrl = defaultIcon
    let icon192Url = defaultIcon
    let icon512Url = "/icons/web-app-manifest-512x512.png"

    if (logo && logo.trim() !== "") {
      iconUrl = `/${name}/api/logo?url=${encodeURIComponent(logo)}&size=192`
      icon192Url = `/${name}/api/logo?url=${encodeURIComponent(logo)}&size=192`
      icon512Url = `/${name}/api/logo?url=${encodeURIComponent(logo)}&size=512`
    }

    return {
      title,
      description: shop.seoDescription || shop.description || `فروشگاه ${shop.name}`,
      manifest: `/${name}/manifest.webmanifest`,
      icons: {
        icon: [
          { url: iconUrl },
          { url: icon192Url, sizes: "192x192", type: "image/png" },
          { url: icon512Url, sizes: "512x512", type: "image/png" },
        ],
        shortcut: iconUrl,
        apple: iconUrl,
      },
    }
  } catch {
    return {
      title: `${name} - Dashboard`,
      description: `PWA dashboard for ${name}`,
      manifest: `/${name}/manifest.webmanifest`,
      icons: {
        icon: [
          { url: "/icons/web-app-manifest-192x192.png" },
          { url: "/icons/web-app-manifest-192x192.png", sizes: "192x192", type: "image/png" },
          { url: "/icons/web-app-manifest-512x512.png", sizes: "512x512", type: "image/png" },
        ],
        shortcut: "/icons/web-app-manifest-192x192.png",
        apple: "/icons/web-app-manifest-192x192.png",
      },
    }
  }
}

export async function generateViewport({
  params
}: {
  params: Promise<LayoutParams>
}): Promise<Viewport> {
  try {
    const { name } = await params
    const shop = await getShop(name)

    return {
      themeColor: shop.menuColor || '#F4F5F9'
    }
  } catch {
    return {
      themeColor: '#F4F5F9'
    }
  }
}

export default async function Layout({
  children,
  params
}: {
  children: React.ReactNode
  params: Promise<LayoutParams>
}): Promise<React.ReactNode> {
  try {
    const { name } = await params
    await getShop(name)
    return <>
      <PreferenceWrapper>{children}</PreferenceWrapper>
      <CartChecker />
      <ActiveChecker />
    </>
  } catch (error) {
    if (error instanceof Error && error.message === 'SHOP_NOT_FOUND') {
      notFound()
    }
    throw error
  }
}
