"use client"

import { Navbar } from "@/components/navbar"
import { Footer } from "@/components/footer"
import { JobCard } from "@/components/job-card"
import { Button } from "@/components/ui/button"
import { Card, CardContent } from "@/components/ui/card"
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"
import { mockJobs, mockTestimonials } from "@/lib/mock-data"
import { FileText, CheckCircle, ArrowRight, Building2, Users, ChevronLeft, ChevronRight } from "lucide-react"
import Link from "next/link"
import { motion } from "framer-motion"
import Image from "next/image"
import { useTranslation } from "@/lib/i18n"
import { useConfig } from "@/contexts/ConfigContext"

import { useState, useEffect } from "react"
import type { Job } from "@/lib/types"

export default function HomePage() {
  const { t } = useTranslation()
  const config = useConfig()
  const [featuredJobs, setFeaturedJobs] = useState<Job[]>([])
  const [loading, setLoading] = useState(true)
  const [featuredIndex, setFeaturedIndex] = useState(0)
  const [moreJobsIndex, setMoreJobsIndex] = useState(0)

  useEffect(() => {
    async function fetchFeaturedJobs() {
      try {
        const apiBase = config.apiUrl
        console.log("[DEBUG] API Base URL:", apiBase)

        const mapJobs = (jobs: any[]): Job[] =>
          jobs.map((j: any) => ({
            id: String(j.id),
            title: j.title,
            company: j.companyName || t("jobs.confidential"),
            location: j.location || t("workMode.remote"),
            type: j.employmentType || "full-time",
            salary: j.salaryMin ? `R$ ${j.salaryMin}` : t("jobs.salary.negotiable"),
            description: j.description,
            requirements: j.requirements || [],
            postedAt: j.createdAt,
            isFeatured: j.isFeatured
          }))

        console.log("[DEBUG] Fetching featured jobs from:", `${apiBase}/api/v1/jobs?featured=true&limit=6`)
        const featuredRes = await fetch(`${apiBase}/api/v1/jobs?featured=true&limit=6`)
        console.log("[DEBUG] Featured response status:", featuredRes.status)

        if (!featuredRes.ok) throw new Error("Failed to fetch featured jobs")
        const featuredData = await featuredRes.json()
        console.log("[DEBUG] Featured data from API:", featuredData)

        const featuredList = featuredData.data ? mapJobs(featuredData.data) : []
        console.log("[DEBUG] Mapped featured jobs:", featuredList.length, "jobs")

        if (featuredList.length === 6) {
          console.log("[DEBUG] Using featured jobs only (6 found)")
          setFeaturedJobs(featuredList)
          return
        }

        console.log("[DEBUG] Fetching fallback jobs from:", `${apiBase}/api/v1/jobs?limit=6`)
        const fallbackRes = await fetch(`${apiBase}/api/v1/jobs?limit=6`)
        console.log("[DEBUG] Fallback response status:", fallbackRes.status)

        if (!fallbackRes.ok) throw new Error("Failed to fetch fallback jobs")
        const fallbackData = await fallbackRes.json()
        console.log("[DEBUG] Fallback data from API:", fallbackData)

        const fallbackList = fallbackData.data ? mapJobs(fallbackData.data) : []
        console.log("[DEBUG] Mapped fallback jobs:", fallbackList.length, "jobs")

        const combined = [...featuredList, ...fallbackList].slice(0, 6)
        console.log("[DEBUG] Combined jobs:", combined.length, "jobs")

        if (combined.length === 6) {
          console.log("[DEBUG] Using combined jobs (6)")
          setFeaturedJobs(combined)
        } else if (combined.length > 0) {
          console.log("[DEBUG] Using combined jobs (less than 6)")
          setFeaturedJobs(combined)
        } else {
          console.log("[DEBUG] ⚠️ USING MOCK DATA - No API jobs found!")
          setFeaturedJobs(mockJobs.slice(0, 6))
        }
      } catch (error) {
        console.error("[DEBUG] ❌ Error fetching featured jobs:", error)
        console.log("[DEBUG] ⚠️ USING MOCK DATA due to error")
        setFeaturedJobs(mockJobs.slice(0, 6))
      } finally {
        setLoading(false)
      }
    }
    fetchFeaturedJobs()
  }, [])

  return (
    <div className="min-h-screen flex flex-col py-2.5">
      <Navbar />

      <main className="flex-1">
        {/* Hero Section */}
        <section className="bg-primary text-white relative overflow-hidden flex items-center min-h-[640px] mb-12">
          <div className="absolute inset-0 z-0">
            {/* Desktop */}
            <Image
              src="/vaga2.png"
              alt="Background"
              fill
              className="hidden md:block object-cover object-center"
              quality={100}
              priority
              sizes="100vw"
            />
            {/* Mobile */}
            <Image
              src="/vagamobile2.png"
              alt="Background"
              fill
              className="block md:hidden object-cover object-center"
              quality={100}
              priority
              sizes="100vw"
            />
          </div>
          <div className="container mx-auto px-4 sm:px-6 lg:px-8 w-full relative z-10">
            <div className="max-w-7xl mx-auto">
              <div className="text-left max-w-2xl">
                <motion.h1
                  initial={{ opacity: 0, y: 20 }}
                  animate={{ opacity: 1, y: 0 }}
                  transition={{ duration: 0.5 }}
                  className="text-4xl sm:text-5xl lg:text-5xl font-bold mb-6"
                >
                  {t('home.hero.title')}<br/>{t('home.hero.titleLine2')}
                </motion.h1>
                <motion.p
                  initial={{ opacity: 0, y: 20 }}
                  animate={{ opacity: 1, y: 0 }}
                  transition={{ duration: 0.5, delay: 0.1 }}
                  className="text-lg mb-8 leading-relaxed text-white/90"
                >
                  {t('home.hero.subtitle')}
                </motion.p>
                <motion.div
                  initial={{ opacity: 0, y: 20 }}
                  animate={{ opacity: 1, y: 0 }}
                  transition={{ duration: 0.5, delay: 0.2 }}
                  className="flex gap-4"
                >
                  <Link href="/jobs">
                    <Button size="lg" className="bg-white text-primary hover:bg-white/95 hover:shadow-lg font-semibold px-8 py-6 text-base rounded-lg transition-all duration-200">
                      {t('home.hero.cta')}
                    </Button>
                  </Link>
                </motion.div>
              </div>
            </div>
          </div>
        </section>

        {/* Search Bar Section */}
        <section className="bg-white py-12">
          <div className="container mx-auto px-4 sm:px-6 lg:px-8 max-w-6xl">
            <div className="bg-white rounded-xl shadow-md p-8">
              <div className="flex items-center gap-3 mb-6">
                <div className="flex-1 relative">
                  <svg className="absolute left-4 top-1/2 -translate-y-1/2 w-5 h-5 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
                  </svg>
                  <input
                    type="text"
                    placeholder={t('home.search.placeholder')}
                    className="w-full pl-12 pr-4 py-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-primary focus:border-transparent text-sm"
                  />
                </div>
                <Button className="bg-primary hover:bg-primary/90 text-white px-8 py-3 rounded-lg font-medium flex items-center gap-2">
                  <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M3 4a1 1 0 011-1h16a1 1 0 011 1v2.586a1 1 0 01-.293.707l-6.414 6.414a1 1 0 00-.293.707V17l-4 4v-6.586a1 1 0 00-.293-.707L3.293 7.293A1 1 0 013 6.586V4z" />
                  </svg>
                  {t('home.search.filter')}
                </Button>
              </div>
              
              <div className="grid grid-cols-1 md:grid-cols-4 gap-6">
                <div className="border border-gray-300 rounded-lg p-4">
                  <button className="flex items-center justify-between w-full mb-3">
                    <span className="text-sm font-semibold text-gray-900">{t('home.search.contractType')}</span>
                    <svg className="w-4 h-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                      <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 9l-7 7-7-7" />
                    </svg>
                  </button>
                  <div className="space-y-2">
                    <label className="flex items-center text-sm text-gray-700 cursor-pointer hover:text-gray-900">
                      <input type="checkbox" className="w-4 h-4 text-primary border-gray-300 rounded focus:ring-primary mr-2" />
                      <span>{t('home.search.pj')}</span>
                    </label>
                    <label className="flex items-center text-sm text-gray-700 cursor-pointer hover:text-gray-900">
                      <input type="checkbox" className="w-4 h-4 text-primary border-gray-300 rounded focus:ring-primary mr-2" />
                      <span>{t('home.search.clt')}</span>
                    </label>
                    <label className="flex items-center text-sm text-gray-700 cursor-pointer hover:text-gray-900">
                      <input type="checkbox" className="w-4 h-4 text-primary border-gray-300 rounded focus:ring-primary mr-2" />
                      <span>{t('home.search.freelancer')}</span>
                    </label>
                  </div>
                </div>
                
                <div className="border border-gray-300 rounded-lg p-4">
                  <button className="flex items-center justify-between w-full mb-3">
                    <span className="text-sm font-semibold text-gray-900">{t('home.search.workMode')}</span>
                    <svg className="w-4 h-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                      <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 9l-7 7-7-7" />
                    </svg>
                  </button>
                  <div className="space-y-2">
                    <label className="flex items-center text-sm text-gray-700 cursor-pointer hover:text-gray-900">
                      <input type="checkbox" className="w-4 h-4 text-primary border-gray-300 rounded focus:ring-primary mr-2" />
                      <span>{t('home.search.homeOffice')}</span>
                    </label>
                    <label className="flex items-center text-sm text-gray-700 cursor-pointer hover:text-gray-900">
                      <input type="checkbox" className="w-4 h-4 text-primary border-gray-300 rounded focus:ring-primary mr-2" />
                      <span>{t('home.search.presencial')}</span>
                    </label>
                    <label className="flex items-center text-sm text-gray-700 cursor-pointer hover:text-gray-900">
                      <input type="checkbox" className="w-4 h-4 text-primary border-gray-300 rounded focus:ring-primary mr-2" />
                      <span>{t('home.search.hybrid')}</span>
                    </label>
                  </div>
                </div>
                
                <div className="border border-gray-300 rounded-lg p-4">
                  <button className="flex items-center justify-between w-full mb-3">
                    <span className="text-sm font-semibold text-gray-900">{t('home.search.location')}</span>
                    <svg className="w-4 h-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                      <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 9l-7 7-7-7" />
                    </svg>
                  </button>
                  <select className="w-full px-3 py-2 text-sm text-gray-500 bg-gray-50 border border-gray-200 rounded focus:outline-none focus:ring-2 focus:ring-primary">
                    <option>{t('home.search.select')}</option>
                  </select>
                </div>
                
                <div className="border border-gray-300 rounded-lg p-4">
                  <button className="flex items-center justify-between w-full mb-3">
                    <span className="text-sm font-semibold text-gray-900">{t('home.search.salary')}</span>
                    <svg className="w-4 h-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                      <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 9l-7 7-7-7" />
                    </svg>
                  </button>
                  <select className="w-full px-3 py-2 text-sm text-gray-500 bg-gray-50 border border-gray-200 rounded focus:outline-none focus:ring-2 focus:ring-primary">
                    <option>{t('home.search.select')}</option>
                  </select>
                </div>
              </div>
            </div>
          </div>
        </section>

        {/* Featured Jobs */}
        <section className="bg-white py-12">
          <div className="container mx-auto px-4 sm:px-6 lg:px-8 max-w-6xl">
            <div className="flex justify-between items-center mb-8">
              <h2 className="text-2xl font-bold text-gray-900">{t('home.featuredJobs.title')}</h2>
              <div className="flex gap-2">
                <Button
                  variant="outline"
                  size="icon"
                  onClick={() => setFeaturedIndex(Math.max(0, featuredIndex - 4))}
                  disabled={featuredIndex === 0}
                  className="h-10 w-10"
                >
                  <ChevronLeft className="h-5 w-5" />
                </Button>
                <Button
                  variant="outline"
                  size="icon"
                  onClick={() => {
                    const jobs = featuredJobs.length >= 8 ? featuredJobs.slice(0, 8) : mockJobs.slice(0, 8)
                    setFeaturedIndex(Math.min(jobs.length - 4, featuredIndex + 4))
                  }}
                  disabled={featuredIndex >= ((featuredJobs.length >= 8 ? featuredJobs.slice(0, 8) : mockJobs.slice(0, 8)).length - 4)}
                  className="h-10 w-10"
                >
                  <ChevronRight className="h-5 w-5" />
                </Button>
              </div>
            </div>

            <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
              {(featuredJobs.length >= 8 ? featuredJobs.slice(0, 8) : mockJobs.slice(0, 8))
                .slice(featuredIndex, featuredIndex + 4)
                .map((job, index) => {
                  const dates = ['02/06', '05/06', '08/06', '11/06', '14/06', '17/06', '19/06', '20/06'];
                  const randomDate = dates[(featuredIndex + index) % dates.length];
                  return (
                  <motion.div
                    key={job.id}
                    initial={{ opacity: 0, y: 20 }}
                    animate={{ opacity: 1, y: 0 }}
                    transition={{ duration: 0.3 }}
                  >
                    <Card className="hover:shadow-lg transition-shadow border-0 rounded-xl overflow-hidden bg-white">
                      <div className="bg-gradient-to-br from-gray-800 to-gray-900 p-8 flex items-center justify-center rounded-t-xl">
                        <div className="w-16 h-16 bg-white rounded-lg flex items-center justify-center">
                          <Building2 className="w-8 h-8 text-gray-900" />
                        </div>
                      </div>
                        
                        {/* Conteúdo do card */}
                        <div className="p-4">
                          <h3 className="font-bold text-base mb-3 text-gray-900 line-clamp-2">{job.title}</h3>
                          
                          {/* Tags */}
                          <div className="flex flex-wrap gap-2 mb-4">
                            <span className="text-xs px-2 py-1 bg-blue-50 text-blue-700 rounded-md border border-blue-200">
                              {job.company}
                            </span>
                            <span className="text-xs px-2 py-1 bg-blue-50 text-blue-700 rounded-md border border-blue-200">
                              {job.location}
                            </span>
                          </div>
                          
                          {/* Data */}
                          <div className="flex items-center gap-1 mb-4 text-gray-500">
                            <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                              <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z" />
                            </svg>
                            <span className="text-xs">{randomDate}</span>
                          </div>
                          
                          {/* Botão */}
                          <Link href={`/jobs/${job.id}`}>
                            <Button size="sm" className="w-full bg-primary hover:bg-primary/90 text-white rounded-md font-semibold">
                              {t('home.featuredJobs.apply')}
                            </Button>
                          </Link>
                        </div>
                      </Card>
                  </motion.div>
                )})}
            </div>
          </div>
        </section>

        {/* More Jobs Section */}
        <section className="bg-white py-12">
          <div className="container mx-auto px-4 sm:px-6 lg:px-8 max-w-6xl">
            <div className="flex justify-between items-center mb-6">
              <h2 className="text-2xl font-bold text-gray-900">{t('home.moreJobs.title')}</h2>
              <Link href="/jobs">
                <Button className="bg-primary hover:bg-primary/90 text-white rounded-md px-6">{t('home.moreJobs.viewAll')}</Button>
              </Link>
            </div>

            <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
              {(featuredJobs.length >= 12 ? featuredJobs.slice(8, 12) : mockJobs.slice(0, 4))
                .map((job, index) => {
                  const dates = ['03/06', '07/06', '10/06', '13/06', '16/06', '18/06'];
                  const randomDate = dates[index % dates.length];
                  return (
                  <motion.div
                    key={job.id}
                    initial={{ opacity: 0, y: 20 }}
                    animate={{ opacity: 1, y: 0 }}
                    transition={{ duration: 0.3 }}
                  >
                    <Card className="hover:shadow-md transition-shadow border border-gray-200 bg-white">
                      <CardContent className="p-5">
                        <div className="flex gap-4">
                          {/* Logo da empresa */}
                          <div className="flex-shrink-0">
                            <Building2 className="w-12 h-12 text-gray-900" />
                          </div>
                          
                          {/* Informações da vaga */}
                          <div className="flex-1 min-w-0">
                            <div className="flex items-start justify-between mb-2">
                              <div className="flex-1">
                                <h3 className="font-semibold text-base mb-1 text-gray-900">{job.title}</h3>
                                <p className="text-sm text-gray-600 mb-2">{job.company}</p>
                                <div className="flex flex-wrap gap-2 mb-3">
                                  <span className="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-blue-100 text-blue-800">
                                    {job.location}
                                  </span>
                                  <span className="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800">
                                    {job.type}
                                  </span>
                                </div>
                              </div>
                              <ChevronRight className="w-5 h-5 text-gray-400 flex-shrink-0 ml-2" />
                            </div>
                            
                            <div className="flex items-center justify-between">
                              <div className="flex items-center gap-1 text-gray-500">
                                <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z" />
                                </svg>
                                <span className="text-xs">{randomDate}</span>
                              </div>
                              <Link href={`/jobs/${job.id}`}>
                                <Button size="sm" className="bg-primary hover:bg-primary/90 text-white rounded-md">
                                  {t('home.featuredJobs.apply')}
                                </Button>
                              </Link>
                            </div>
                          </div>
                        </div>
                      </CardContent>
                    </Card>
                  </motion.div>
                )})}
            </div>
          </div>
        </section>

        {/* CTA Section */}
        <section className="py-12 bg-white">
          <div className="container mx-auto px-4 sm:px-6 lg:px-8 max-w-8xl">
            <div className="bg-primary rounded-2xl overflow-hidden shadow-lg relative">
              <div className="absolute inset-0 z-0">
                <Image
                  src="/optr1.png"
                  alt="Background"
                  fill
                  className="object-cover object-right"
                  quality={100}
                />
              </div>
              <div className="grid lg:grid-cols-2 gap-8 items-center p-8 lg:p-12 relative z-10">
                {/* Text Content */}
                <div className="text-white">
                  <div className="flex items-center gap-2 mb-6">
                    <Users className="w-6 h-6" />
                    <span className="font-medium text-sm">{t('home.cta.badge')}</span>
                  </div>
                  <h2 className="text-3xl lg:text-4xl font-bold mb-4 leading-tight">
                    {t('home.cta.title')}
                  </h2>
                  <p className="mb-6 text-white/90 text-sm">
                    {t('home.cta.subtitle')}
                  </p>
                  <Button size="lg" className="bg-white text-primary hover:bg-white/90 font-semibold px-8 rounded-md">
                    {t('home.cta.button')}
                  </Button>
                </div>
              </div>
            </div>
          </div>
        </section>
      </main>

      <Footer />
    </div>
  )
}
