'use client';

import Link from 'next/link';
import { useEffect, useState } from 'react';
import CareersHeader from '@/components/careers/CareersHeader';
import CareersFooter from '@/components/careers/CareersFooter';
import { fetchPublicJobs, type PublicJob } from '@/lib/careers';

export default function CareersLanding() {
  const [jobs, setJobs] = useState<PublicJob[]>([]);
  const [loading, setLoading] = useState(true);
  const [q, setQ] = useState('');

  useEffect(() => {
    fetchPublicJobs()
      .then(setJobs)
      .catch(() => setJobs([]))
      .finally(() => setLoading(false));
  }, []);

  const stats = [
    { n: jobs.length, l: 'Open positions', icon: 'fa-briefcase' },
    { n: new Set(jobs.map((j) => j.location).filter(Boolean)).size, l: 'Locations', icon: 'fa-location-dot' },
    { n: new Set(jobs.map((j) => j.department).filter(Boolean)).size, l: 'Departments', icon: 'fa-layer-group' },
  ];

  return (
    <div className="min-h-screen flex flex-col bg-[#f3f3f9]">
      <CareersHeader />

      {/* ---------------------------------------------------------------- Hero */}
      <section className="relative overflow-hidden text-white">
        {/* layered brand gradient */}
        <div
          className="absolute inset-0"
          style={{ background: 'linear-gradient(135deg,#2b3566 0%,#405189 45%,#23b7e5 100%)' }}
        />
        {/* subtle dotted texture */}
        <div
          className="absolute inset-0 opacity-[0.15]"
          style={{
            backgroundImage: 'radial-gradient(#ffffff 1px, transparent 1px)',
            backgroundSize: '22px 22px',
          }}
        />
        {/* soft glow accents */}
        <div className="absolute -top-24 -right-24 w-96 h-96 rounded-full bg-white/10 blur-3xl" />
        <div className="absolute -bottom-32 -left-16 w-96 h-96 rounded-full bg-[#23b7e5]/20 blur-3xl" />

        <div className="relative max-w-6xl mx-auto px-4 sm:px-6 py-16 sm:py-24 text-center">
          <span className="inline-flex items-center gap-2 px-3 py-1 rounded-full bg-white/15 backdrop-blur text-xs font-semibold tracking-wide uppercase ring-1 ring-white/20">
            <span className="w-1.5 h-1.5 rounded-full bg-emerald-300 animate-pulse" />
            We&apos;re hiring
          </span>

          <h1 className="mt-6 text-4xl sm:text-6xl font-extrabold leading-[1.1] tracking-tight">
            Find your next role at
            <br className="hidden sm:block" /> <span className="text-transparent bg-clip-text bg-gradient-to-r from-white to-sky-200">Indovision</span>
          </h1>
          <p className="mt-5 text-base sm:text-lg text-white/80 max-w-2xl mx-auto leading-relaxed">
            Browse open positions, upload your résumé once, and apply in a single click.
          </p>

          {/* search */}
          <form
            onSubmit={(e) => { e.preventDefault(); window.location.href = `/careers/jobs?q=${encodeURIComponent(q)}`; }}
            className="mt-9 max-w-xl mx-auto flex items-center rounded-full bg-white p-1.5 shadow-2xl shadow-black/20 ring-1 ring-white/40"
          >
            <span className="hidden sm:flex pl-4 pr-1 text-slate-400">
              <i className="fa-solid fa-magnifying-glass" />
            </span>
            <input
              value={q}
              onChange={(e) => setQ(e.target.value)}
              placeholder="Search by title, skill or location…"
              className="flex-1 min-w-0 px-4 sm:px-2 py-3 text-slate-800 text-sm bg-transparent focus:outline-none"
            />
            <button
              type="submit"
              aria-label="Search"
              className="shrink-0 px-4 sm:px-6 py-3 rounded-full bg-[#405189] text-white font-semibold text-sm hover:bg-[#334267] active:scale-[0.98] transition"
            >
              <i className="fa-solid fa-magnifying-glass sm:hidden" />
              <span className="hidden sm:inline">Search</span>
            </button>
          </form>

          <div className="mt-7 flex flex-wrap items-center justify-center gap-3">
            <Link
              href="/careers/jobs"
              className="px-6 py-3 rounded-full bg-white text-[#405189] font-semibold text-sm shadow-lg shadow-black/10 hover:bg-slate-100 active:scale-[0.98] transition"
            >
              View all jobs
            </Link>
            <Link
              href="/register"
              className="px-6 py-3 rounded-full border border-white/60 text-white font-semibold text-sm hover:bg-white/10 active:scale-[0.98] transition"
            >
              Create profile
            </Link>
          </div>
        </div>

        {/* wave divider */}
        <svg className="relative block w-full h-[40px] text-[#f3f3f9]" viewBox="0 0 1440 40" preserveAspectRatio="none" aria-hidden>
          <path fill="currentColor" d="M0,40 L1440,40 L1440,10 C1080,34 360,34 0,10 Z" />
        </svg>
      </section>

      {/* ---------------------------------------------------------------- Stats */}
      <section className="max-w-6xl mx-auto w-full px-4 sm:px-6 -mt-12 relative z-10">
        <div className="grid grid-cols-1 sm:grid-cols-3 gap-4">
          {stats.map((s) => (
            <div
              key={s.l}
              className="flex items-center gap-4 bg-white rounded-xl border border-slate-200/80 shadow-sm px-6 py-5 hover:shadow-md transition"
            >
              <span className="w-12 h-12 shrink-0 rounded-xl bg-[#405189]/10 text-[#405189] flex items-center justify-center text-lg">
                <i className={`fa-solid ${s.icon}`} />
              </span>
              <div>
                <div className="text-2xl font-extrabold text-slate-800 leading-none">{s.n}</div>
                <div className="text-xs font-semibold text-slate-500 uppercase tracking-wide mt-1.5">{s.l}</div>
              </div>
            </div>
          ))}
        </div>
      </section>

      {/* ---------------------------------------------------------------- Latest jobs */}
      <section className="max-w-6xl mx-auto w-full px-4 sm:px-6 py-14">
        <div className="flex flex-col sm:flex-row sm:items-end sm:justify-between gap-2 mb-7">
          <div>
            <h2 className="text-xl sm:text-2xl font-extrabold text-slate-800 tracking-tight">Latest openings</h2>
            <p className="text-sm text-slate-500 mt-1">Fresh roles across our teams — apply in minutes.</p>
          </div>
          <Link href="/careers/jobs" className="shrink-0 text-sm font-semibold text-[#405189] hover:underline">
            See all <i className="fa-solid fa-arrow-right ml-1 text-xs" />
          </Link>
        </div>

        {loading ? (
          <div className="grid gap-5 sm:grid-cols-2 lg:grid-cols-3">
            {Array.from({ length: 6 }).map((_, i) => (
              <div key={i} className="bg-white rounded-xl border border-slate-200 p-5 animate-pulse">
                <div className="h-4 w-2/3 bg-slate-200 rounded" />
                <div className="h-3 w-1/2 bg-slate-100 rounded mt-3" />
                <div className="flex gap-2 mt-5">
                  <div className="h-6 w-20 bg-slate-100 rounded-full" />
                  <div className="h-6 w-16 bg-slate-100 rounded-full" />
                </div>
              </div>
            ))}
          </div>
        ) : jobs.length === 0 ? (
          <div className="bg-white rounded-xl border border-dashed border-slate-300 py-16 text-center">
            <i className="fa-regular fa-folder-open text-3xl text-slate-300" />
            <p className="text-slate-500 text-sm mt-3">No open positions right now. Please check back soon.</p>
          </div>
        ) : (
          <div className="grid gap-5 sm:grid-cols-2 lg:grid-cols-3">
            {jobs.slice(0, 6).map((j) => (
              <Link
                key={j.id}
                href={`/careers/jobs/${j.public_id || j.id}`}
                className="group flex flex-col bg-white rounded-xl border border-slate-200 p-5 shadow-sm hover:shadow-lg hover:-translate-y-0.5 hover:border-[#405189]/40 transition-all duration-200"
              >
                <div className="flex items-start gap-3">
                  <span className="w-10 h-10 shrink-0 rounded-lg bg-gradient-to-br from-[#405189] to-[#23b7e5] text-white flex items-center justify-center font-bold text-sm">
                    {(j.title || '?').charAt(0).toUpperCase()}
                  </span>
                  <div className="min-w-0">
                    <h3 className="font-bold text-slate-800 leading-snug group-hover:text-[#405189] transition line-clamp-2">
                      {j.title}
                    </h3>
                    <p className="text-xs text-slate-500 mt-0.5 truncate">
                      {j.client_name || 'Indovision'}{j.department ? ` · ${j.department}` : ''}
                    </p>
                  </div>
                </div>

                <div className="flex flex-wrap gap-2 mt-4">
                  {j.location && (
                    <span className="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full bg-slate-100 text-xs font-medium text-slate-600">
                      <i className="fa-solid fa-location-dot text-[#405189]" />{j.location}
                    </span>
                  )}
                  {j.experience_band && (
                    <span className="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full bg-slate-100 text-xs font-medium text-slate-600">
                      <i className="fa-solid fa-briefcase text-[#405189]" />{j.experience_band}
                    </span>
                  )}
                </div>

                <div className="mt-auto pt-4 flex items-center text-sm font-semibold text-[#405189]">
                  View role
                  <i className="fa-solid fa-arrow-right ml-2 text-xs group-hover:translate-x-1 transition-transform" />
                </div>
              </Link>
            ))}
          </div>
        )}
      </section>

      {/* ---------------------------------------------------------------- Value props */}
      <section className="max-w-6xl mx-auto w-full px-4 sm:px-6 pb-16">
        <div className="grid gap-5 sm:grid-cols-3">
          {[
            { icon: 'fa-file-arrow-up', t: 'Apply once', d: 'Upload your résumé a single time and reuse it across every role.' },
            { icon: 'fa-list-check', t: 'Track your status', d: 'Follow each application through every stage of the pipeline.' },
            { icon: 'fa-bolt', t: 'Fast response', d: 'Our recruiters review new applications quickly, every day.' },
          ].map((f) => (
            <div key={f.t} className="bg-white rounded-xl border border-slate-200 p-6 shadow-sm">
              <span className="w-11 h-11 rounded-lg bg-[#405189]/10 text-[#405189] flex items-center justify-center text-lg">
                <i className={`fa-solid ${f.icon}`} />
              </span>
              <h3 className="font-bold text-slate-800 mt-4">{f.t}</h3>
              <p className="text-sm text-slate-500 mt-1.5 leading-relaxed">{f.d}</p>
            </div>
          ))}
        </div>
      </section>

      <div className="mt-auto">
        <CareersFooter />
      </div>
    </div>
  );
}
