import { Fragment, useEffect, useState } from 'react';
import Head from 'next/head';

import { useRouter } from 'next/router';

import Header from '@/components/templates/layout/header';
import Footer from '@/components/templates/layout/Footer';
import { getCities } from '@/data/cities';
import { GetStaticPropsContext, NextPage } from 'next';
import { serverSideTranslations } from "next-i18next/serverSideTranslations";
import { useTranslation } from "next-i18next";
import Link from 'next/link';

const Page: NextPage = () => {
  const router = useRouter();
  const { t } = useTranslation(['common']);
  const [cities, setCities] = useState<any[]>([]);

  useEffect(() => {
    const fetchCities = async () => {
      try {
        const data: any = await getCities(router.locale || 'es');
        setCities(data);
      } catch (error) {
        console.error('Error cargando ciudades:', error);
      }
    };

    fetchCities();
  }, [router.locale]);

  return (
    <Fragment>
      <Head>
        <title>Grupo Velas | 404</title>
        <meta name="robots" content="noindex, nofollow" />
      </Head>

      <Header categories={cities} />

      <main className="tw-flex tw-flex-col tw-items-center tw-justify-center tw-min-h-[60vh] tw-text-center tw-p-6">
        <p className=" tw-text-base md:tw-text-xl  tw-text-main-color tw-font-ibm tw-font-semibold  tw-uppercase tw-text-center">
          Error
        </p>
        <h1 className="tw-text-center tw-text-5xl md:tw-text-7xl tw-font-normal tw-text-main-dark tw-font-ibm tw-my-4">
          404
        </h1>
        <p className="base-text text-center mb-6">
          {router?.locale === "es" ? "¡Ups! Página no encontrada." : "Oops! Page not found."}
        </p>
        <Link
          href="/"
          className="tw-rounded-md hover:tw-bg-[#373A36] hover:tw-text-white tw-text-white tw-font-semibold tw-font-inter tw-text-xs tw-py-4 tw-px-8 tw-uppercase  tw-bg-main-color "
        >
          {router?.locale === "es" ? "Ir al inicio" : "Go to home"}
        </Link>
      </main>

      <Footer cities={cities} />
    </Fragment>
  );
};
export async function getStaticProps({ locale }: GetStaticPropsContext) {
  return {
    props: {
      ...(await serverSideTranslations(locale ?? "es", [
        "common",
        "routes"
      ])),
    },
  };
}
export default Page;
