import Navbar          from "@/components/Navbar";
import Hero            from "@/components/Hero";
import Ticker          from "@/components/Ticker";
import ProductShowcase from "@/components/ProductShowcase";
import VideoSection    from "@/components/VideoSection";
import Features        from "@/components/Features";
import MapSection      from "@/components/MapSection";
import Footer          from "@/components/Footer";
import pool, { initDB } from "@/lib/db";
import type { Product } from "@/types/product";

export const revalidate = 3600;

async function getProducts(): Promise<Product[]> {
  try {
    await initDB();
    const SHOWCASE = [
      "Njuumaan",
      "Farine de Céréales Cuite",
      "Tejmakhtt Plus",
      "Tiguigerté",
      "Moutcha",
    ];
    const { rows } = await pool.query(
      `SELECT * FROM products WHERE name = ANY($1) AND available = true`,
      [SHOWCASE]
    );
    rows.sort((a, b) => SHOWCASE.indexOf(a.name) - SHOWCASE.indexOf(b.name));
    return rows;
  } catch {
    return [];
  }
}

export default async function Home() {
  const products = await getProducts();

  return (
    <>
      <Navbar />
      <Hero />
      <Ticker />
      <ProductShowcase products={products} />
      <VideoSection />
      <Features />
      <MapSection />
      <Footer />
    </>
  );
}
