import Head from "next/head";

import HomeSlider from "@/components/Commen/HomeSlider";
import NotificationBar from "@/components/Commen/NotificationBar";
import HomePageNewComponent from "@/components/UpdateBharat/HomePageNewComponent";
import { URL } from "@/components/url";

/* ================= SEO ================= */
export default function Home({ newsList, categoriesList, sliderList }) {
  return (
    <>
      <Head>
        <title>
          Latest News in India | Breaking Headlines & Updates | Update Bharat
        </title>
        <meta
          name="description"
          content="Get the latest breaking news, trending stories, politics, entertainment, sports, business, and technology updates from across India. Stay informed with Update Bharat – India’s trusted digital news platform."
        />

        {/* Open Graph */}
        <meta
          property="og:title"
          content="Update Bharat – India’s Trusted News Portal for Real & Fast Updates"
        />
        <meta
          property="og:description"
          content="Stay up to date with breaking news, current affairs, and viral stories from all over India."
        />
        <meta property="og:url" content="https://updatebharat.com/" />
        <meta property="og:type" content="website" />
        <meta property="og:locale" content="en_IN" />
        <meta
          property="og:image"
          content="https://updatebharat.com/logo.png"
        />

        {/* Twitter */}
        <meta name="twitter:card" content="summary_large_image" />
        <meta
          name="twitter:title"
          content="Update Bharat – India’s Trusted News Portal"
        />
        <meta
          name="twitter:description"
          content="Read the latest news and updates from across India on Update Bharat."
        />
        <meta
          name="twitter:image"
          content="https://updatebharat.com/logo.png"
        />

        <link rel="canonical" href="https://updatebharat.com/" />
      </Head>

      <div className="container-fluid px-0">
        <HomeSlider sliderList={sliderList} />
        <NotificationBar />
        <HomePageNewComponent
          categoriesList={categoriesList}
          newsList={newsList}
        />
      </div>
    </>
  );
}

/* ================= DATA FETCHING ================= */
export async function getServerSideProps() {
  try {
    const res = await fetch(
      `${URL.API_BASE_URL}/publicApi/getNewListPub1/1?currentPage=home`
    );

    if (!res.ok) {
      throw new Error("Failed to fetch home data");
    }

    const json = await res.json();
    const data = json?.data || {};

    return {
      props: {
        newsList: data.newsList || [],
        categoriesList: data.categoryList || [],
        sliderList: data.sliderList || [],
      },
    };
  } catch (error) {
    console.error("Home page fetch error:", error);

    return {
      props: {
        newsList: [],
        categoriesList: [],
        sliderList: [],
      },
    };
  }
}
