import { Hero } from "@/components/landing/Hero";
import { TopCompanies } from "@/components/landing/TopCompanies";
import { Categories } from "@/components/landing/Categories";
import { TrustedBy } from "@/components/landing/TrustedBy";
import { Features } from "@/components/landing/Features";
import { HowItWorks } from "@/components/landing/HowItWorks";
import { TrendingJobs } from "@/components/landing/TrendingJobs";
import { JobService } from "@/services/JobService";
import { Footer } from "@/components/landing/Footer";
import { Suspense } from "react";
import { SiteHeader } from "@/components/shared/SiteHeader";

export default async function Home() {
  const popularData = await JobService.getPopularCategories();
  const trendingJobsResult = await JobService.getJobs({ sort: 'recent', pageSize: 6 });

  // ─── Static fallbacks (shown when DB has no data yet) ─────────────────────
  const FALLBACK_COMPANIES = [
    { id: 'google', name: 'Google', logo: null, openPositions: 12 },
    { id: 'microsoft', name: 'Microsoft', logo: null, openPositions: 8 },
    { id: 'meta', name: 'Meta', logo: null, openPositions: 6 },
    { id: 'amazon', name: 'Amazon', logo: null, openPositions: 15 },
  ];
  const FALLBACK_CATEGORIES = [
    { id: 'analytics', name: 'Analytics', count: 1 },
    { id: 'banking', name: 'Banking & Finance', count: 8 },
    { id: 'data-science', name: 'Data Scientist', count: 2 },
    { id: 'design', name: 'Design & Creative', count: 4 },
    { id: 'development', name: 'Development & IT', count: 4 },
    { id: 'finance', name: 'Finance & Accounting', count: 2 },
    { id: 'graduate', name: 'Graduate Jobs', count: 1 },
    { id: 'hr', name: 'HR', count: 5 },
  ];
  const FALLBACK_JOBS = [
    { id: 'job-1', title: 'Senior Backend Engineer', company: 'Google', location: 'Lagos, Nigeria', type: 'full-time', salaryMin: 1500, salaryMax: 3000, currency: 'USD', postedAt: new Date(Date.now() - 2 * 86400000).toISOString() },
    { id: 'job-2', title: 'Product Designer', company: 'Microsoft', location: 'Remote', type: 'full-time', salaryMin: 1200, salaryMax: 2500, currency: 'USD', postedAt: new Date(Date.now() - 5 * 86400000).toISOString() },
    { id: 'job-3', title: 'Data Scientist', company: 'Meta', location: 'Abuja, Nigeria', type: 'contract', salaryMin: 2000, salaryMax: 4000, currency: 'USD', postedAt: new Date(Date.now() - 1 * 86400000).toISOString() },
    { id: 'job-4', title: 'Frontend Engineer', company: 'Paystack', location: 'Lagos, Nigeria', type: 'full-time', salaryMin: 800, salaryMax: 1800, currency: 'USD', postedAt: new Date(Date.now() - 3 * 86400000).toISOString() },
    { id: 'job-5', title: 'DevOps Engineer', company: 'Flutterwave', location: 'Remote', type: 'full-time', salaryMin: 1500, salaryMax: 3000, currency: 'USD', postedAt: new Date(Date.now() - 7 * 86400000).toISOString() },
    { id: 'job-6', title: 'Marketing Manager', company: 'Jumia', location: 'Lagos, Nigeria', type: 'full-time', salaryMin: 600, salaryMax: 1200, currency: 'USD', postedAt: new Date(Date.now() - 4 * 86400000).toISOString() },
  ];

  const displayCompanies = popularData.featuredEmployers.length > 0
    ? popularData.featuredEmployers
    : FALLBACK_COMPANIES;

  const displayCategories = popularData.industries.length > 0
    ? popularData.industries.map(i => ({ id: i.id, name: i.name, count: i.count }))
    : FALLBACK_CATEGORIES;

  const displayJobs = trendingJobsResult.jobs.length > 0
    ? trendingJobsResult.jobs
    : FALLBACK_JOBS;

  return (
    <main className="min-h-screen bg-background-light overflow-x-hidden relative font-display">
      <SiteHeader />

      <Suspense fallback={<div className="h-96 w-full animate-pulse bg-slate-100" />}>
        <Hero />
      </Suspense>

      {/* 1. Top Companies Hiring */}
      <TopCompanies companies={displayCompanies} />

      {/* 2. Job Categories */}
      <Categories categories={displayCategories} />

      {/* 3. How It Works */}
      <HowItWorks />

      {/* 4. Featured Jobs */}
      <TrendingJobs jobs={displayJobs} />

      {/* 5. Trusted By */}
      <TrustedBy />

      {/* 6. Features (For Talent / For Companies) */}
      <Features />

      {/* 7. Footer */}
      <Footer />
    </main>
  );
}
