import React, { useState } from 'react';
import { PublicHeader } from '@/Components/PublicHeader';
import { PublicFooter } from '@/Components/PublicFooter';
import { SEOHead } from '@/Components/SEOHead';
import { ProjectCard } from '@/Components/ProjectCard';
import { Link, usePage } from '@inertiajs/react';
import { FolderGit2, Zap, ShieldCheck, Star, ArrowRight, Layers } from 'lucide-react';

interface ProjectsIndexProps {
    categories: any[];
    projects: any[];
}

export default function Index({ categories, projects }: ProjectsIndexProps) {
    const { globalSettings } = usePage().props as any;
    const [selectedCategory, setSelectedCategory] = useState<string>('all');

    const filteredProjects = selectedCategory === 'all'
        ? projects
        : projects.filter(p => p.category?.slug === selectedCategory);

    const isMetricsVisible = globalSettings?.projects_global_show_metrics !== '0';
    const metricFallback = globalSettings?.projects_metric_fallback || 'tech_stack';

    const statPlatforms = globalSettings?.projects_stat_platforms_delivered || `${Math.max(projects.length * 10, 80)}+`;
    const statUptime = globalSettings?.projects_stat_uptime || '99.99%';
    const statRating = globalSettings?.projects_stat_client_rating || '4.9 ★';

    const statMetricValue = isMetricsVisible
        ? (globalSettings?.projects_stat_processed_revenue || '$250M+')
        : (
            metricFallback === 'industry'
                ? `${Math.max(categories.length * 2, 12)}+`
                : metricFallback === 'status'
                ? '100%'
                : '45+'
        );

    const statMetricLabel = isMetricsVisible
        ? 'Processed Volume'
        : (
            metricFallback === 'industry'
                ? 'Global Industry Domains'
                : metricFallback === 'status'
                ? 'Production Verified Code'
                : 'Core Tech Stacks'
        );

    return (
        <div className="min-h-screen bg-[#060B18] text-slate-100 flex flex-col font-sans">
            <SEOHead
                title="Case Studies & Projects Portfolio | Tech in Soul"
                description="Explore Tech in Soul portfolio of delivered web applications, mobile platforms, e-commerce stores, and SaaS analytics engines."
            />

            <PublicHeader />

            <main className="flex-grow pt-32 pb-20">
                {/* Hero Header */}
                <div className="max-w-[1600px] mx-auto px-4 sm:px-6 lg:px-12 text-center space-y-4 mb-16">
                    <span className="text-xs font-bold text-[#00F2FE] uppercase tracking-widest bg-[#00F2FE]/10 px-3.5 py-1 rounded-full border border-[#00F2FE]/20 inline-block">
                        OUR CASE STUDIES
                    </span>
                    <h1 className="text-4xl sm:text-6xl font-extrabold text-white tracking-tight leading-tight">
                        Proven Software <span className="text-gradient-aqua">Case Studies</span>
                    </h1>
                    <p className="text-slate-400 text-base sm:text-lg max-w-2xl mx-auto leading-relaxed">
                        Explore how our engineering teams deliver scalable digital software, seamless user experiences, and bulletproof microservices across global industries.
                    </p>
                </div>

                {/* Portfolio Stats Bar */}
                <div className="max-w-[1600px] mx-auto px-4 sm:px-6 lg:px-12 mb-16">
                    <div className="grid grid-cols-2 md:grid-cols-4 gap-4 sm:gap-6">
                        <div className="glass-card rounded-2xl p-6 text-center space-y-1 border border-slate-800">
                            <div className="text-3xl font-extrabold text-[#00F2FE] font-mono">{statPlatforms}</div>
                            <div className="text-xs font-bold text-slate-400 uppercase">Delivered Platforms</div>
                        </div>
                        <div className="glass-card rounded-2xl p-6 text-center space-y-1 border border-slate-800">
                            <div className={`text-3xl font-extrabold font-mono ${isMetricsVisible ? 'text-white' : 'text-[#00F2FE]'}`}>
                                {statMetricValue}
                            </div>
                            <div className="text-xs font-bold text-slate-400 uppercase">{statMetricLabel}</div>
                        </div>
                        <div className="glass-card rounded-2xl p-6 text-center space-y-1 border border-slate-800">
                            <div className="text-3xl font-extrabold text-[#00F2FE] font-mono">{statUptime}</div>
                            <div className="text-xs font-bold text-slate-400 uppercase">Average Cloud Uptime</div>
                        </div>
                        <div className="glass-card rounded-2xl p-6 text-center space-y-1 border border-slate-800">
                            <div className="text-3xl font-extrabold text-amber-400 font-mono">{statRating}</div>
                            <div className="text-xs font-bold text-slate-400 uppercase">Client Satisfaction</div>
                        </div>
                    </div>
                </div>



                {/* Category Filter Pills */}
                <div className="max-w-[1600px] mx-auto px-4 sm:px-6 lg:px-12 mb-12">
                    <div className="flex flex-wrap items-center justify-center gap-2 sm:gap-3">
                        <button
                            onClick={() => setSelectedCategory('all')}
                            className={`px-5 py-2.5 rounded-xl text-xs font-bold transition-all ${
                                selectedCategory === 'all'
                                    ? 'bg-[#00F2FE] text-[#060B18] shadow-lg shadow-[#00F2FE]/20'
                                    : 'bg-[#0B132B] text-slate-300 border border-slate-800 hover:border-[#00F2FE]/40'
                            }`}
                        >
                            All Case Studies ({projects.length})
                        </button>
                        {categories.map((cat) => {
                            const count = projects.filter(p => p.category?.slug === cat.slug).length;
                            return (
                                <button
                                    key={cat.id}
                                    onClick={() => setSelectedCategory(cat.slug)}
                                    className={`px-5 py-2.5 rounded-xl text-xs font-bold transition-all ${
                                        selectedCategory === cat.slug
                                            ? 'bg-[#00F2FE] text-[#060B18] shadow-lg shadow-[#00F2FE]/20'
                                            : 'bg-[#0B132B] text-slate-300 border border-slate-800 hover:border-[#00F2FE]/40'
                                    }`}
                                >
                                    {cat.name} ({count})
                                </button>
                            );
                        })}
                    </div>
                </div>

                {/* Projects Grid */}
                <div className="max-w-[1600px] mx-auto px-4 sm:px-6 lg:px-12 mb-24">
                    {filteredProjects.length === 0 ? (
                        <div className="glass-card rounded-3xl p-12 text-center text-slate-400 max-w-lg mx-auto border border-slate-800">
                            <h3 className="text-lg font-bold text-white mb-2">No Case Studies Found</h3>
                            <p className="text-xs text-slate-400">No project case studies are available under this category currently.</p>
                        </div>
                    ) : (
                        <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8">
                            {filteredProjects.map((project) => (
                                <ProjectCard key={project.id} project={project} />
                            ))}
                        </div>
                    )}
                </div>

                {/* Project Contact CTA */}
                <div className="max-w-[1600px] mx-auto px-4 sm:px-6 lg:px-12">
                    <div className="glass-card rounded-3xl p-8 sm:p-12 border border-[#00F2FE]/30 flex flex-col md:flex-row items-center justify-between gap-8">
                        <div className="space-y-3 max-w-2xl">
                            <span className="text-xs font-bold text-[#00F2FE] uppercase tracking-widest">
                                HAVE A PROJECT IN MIND?
                            </span>
                            <h3 className="text-3xl font-bold text-white">Let's Build Your Next Digital Product</h3>
                            <p className="text-xs sm:text-sm text-slate-400 leading-relaxed">
                                Share your requirements with our lead solution architects for a free technical consultation and estimated timeline.
                            </p>
                        </div>
                        <Link href="/contact" className="btn-aqua px-8 py-4 rounded-xl font-bold text-sm shrink-0 flex items-center gap-2">
                            Start Your Project <ArrowRight className="w-4 h-4" />
                        </Link>
                    </div>
                </div>
            </main>

            <PublicFooter />
        </div>
    );
}
