import React from 'react';
import { Link, usePage } from '@inertiajs/react';
import { PublicHeader } from '@/Components/PublicHeader';
import { PublicFooter } from '@/Components/PublicFooter';
import { SEOHead } from '@/Components/SEOHead';
import { ProjectCard } from '@/Components/ProjectCard';
import { ArrowLeft, ExternalLink, Calendar, Building, CheckCircle2, Quote, TrendingUp, Shield, Cpu, Layers } from 'lucide-react';

interface ProjectShowProps {
    project: any;
    relatedProjects: any[];
}

export default function Show({ project, relatedProjects }: ProjectShowProps) {
    const { globalSettings } = usePage().props as any;
    const isUrlVisible = (globalSettings?.projects_global_show_urls !== '0') && (project.show_visit_url !== false) && Boolean(project.project_url);
    const isMetricVisible = (globalSettings?.projects_global_show_metrics !== '0') && (project.show_key_metric !== false) && Boolean(project.key_metric);
    const metricFallback = globalSettings?.projects_metric_fallback || 'tech_stack';
    const urlFallbackText = globalSettings?.projects_url_fallback_text || 'Internal Enterprise System';

    return (
        <div className="min-h-screen bg-[#060B18] text-slate-100 flex flex-col font-sans">
            <SEOHead
                title={`${project.meta_title || project.title} | Case Study Tech in Soul`}
                description={project.meta_description || project.short_description}
            />

            <PublicHeader />

            <main className="flex-grow pt-32 pb-20">
                <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 space-y-8">
                    <Link
                        href="/projects"
                        className="inline-flex items-center gap-2 text-xs font-semibold text-slate-400 hover:text-[#00F2FE] transition-colors"
                    >
                        <ArrowLeft className="w-4 h-4" /> Back to Case Studies
                    </Link>


                    {/* Header Details */}
                    <div>
                        <div className="flex items-center gap-2 mb-2">
                            <span className="text-xs font-bold text-[#00F2FE] uppercase tracking-widest">
                                {project.category?.name || 'CASE STUDY'}
                            </span>
                            {project.industry && (
                                <span className="text-xs text-slate-400">• {project.industry}</span>
                            )}
                        </div>
                        <h1 className="text-4xl sm:text-5xl font-extrabold text-white tracking-tight">
                            {project.title}
                        </h1>
                    </div>

                    {/* Key Attributes Bar */}
                    <div className="glass-card rounded-2xl p-6 grid grid-cols-2 sm:grid-cols-4 gap-6 text-center border border-slate-800">
                        <div>
                            <span className="text-xs text-slate-400 block mb-1">Client Name</span>
                            <span className="text-sm font-bold text-white">{project.client_name || 'Enterprise'}</span>
                        </div>
                        <div>
                            <span className="text-xs text-slate-400 block mb-1">
                                {isMetricVisible ? (
                                    project.key_metric_label || 'Key Metric'
                                ) : metricFallback === 'industry' ? (
                                    'Engineering Domain'
                                ) : metricFallback === 'status' ? (
                                    'Live Deployment'
                                ) : (
                                    'Core Architecture'
                                )}
                            </span>
                            <span className="text-sm font-bold text-[#00F2FE] truncate block px-1">
                                {isMetricVisible ? (
                                    project.key_metric
                                ) : metricFallback === 'industry' ? (
                                    project.industry || project.category?.name || 'Enterprise'
                                ) : metricFallback === 'status' ? (
                                    'Active Cloud Production'
                                ) : (
                                    project.technologies && project.technologies.length > 0
                                        ? project.technologies.slice(0, 2).join(' • ')
                                        : project.industry || 'Full Stack Architecture'
                                )}
                            </span>
                        </div>
                        <div>
                            <span className="text-xs text-slate-400 block mb-1">Completion</span>
                            <span className="text-sm font-bold text-white">
                                {project.completion_date ? new Date(project.completion_date).toLocaleDateString('en-US', { month: 'short', year: 'numeric' }) : '2025'}
                            </span>
                        </div>
                        <div>
                            <span className="text-xs text-slate-400 block mb-1">Deployment Access</span>
                            {isUrlVisible ? (
                                <a
                                    href={project.project_url}
                                    target="_blank"
                                    rel="noreferrer"
                                    className="text-xs font-semibold text-[#00F2FE] hover:underline inline-flex items-center gap-1"
                                >
                                    Visit Site <ExternalLink className="w-3 h-3" />
                                </a>
                            ) : (
                                <span className="text-xs text-slate-400 font-medium inline-flex items-center gap-1">
                                    <Shield className="w-3 h-3 text-[#00F2FE]/70" /> {urlFallbackText}
                                </span>
                            )}
                        </div>
                    </div>

                    {/* Technologies Used Bar */}
                    {project.technologies && project.technologies.length > 0 && (
                        <div className="glass-card rounded-2xl p-5 border border-slate-800 flex flex-wrap items-center gap-2">
                            <span className="text-xs font-bold text-slate-400 uppercase tracking-wider mr-2 flex items-center gap-1.5">
                                <Cpu className="w-3.5 h-3.5 text-[#00F2FE]" /> Core Stack:
                            </span>
                            {project.technologies.map((tech: string, idx: number) => (
                                <span
                                    key={idx}
                                    className="text-xs font-mono font-medium px-3 py-1 rounded-xl bg-[#0B132B] text-[#00F2FE] border border-[#00F2FE]/25 shadow-sm"
                                >
                                    {tech}
                                </span>
                            ))}
                        </div>
                    )}

                    {/* Challenge, Solution, Result Grid */}
                    <div className="space-y-6">

                        <div className="glass-card rounded-2xl p-8 space-y-4">
                            <h3 className="text-xl font-bold text-white flex items-center gap-2">
                                <span className="w-3 h-3 rounded-full bg-amber-400" />
                                The Challenge
                            </h3>
                            <p className="text-sm text-slate-300 leading-relaxed">
                                {project.challenge || project.description}
                            </p>
                        </div>

                        <div className="glass-card rounded-2xl p-8 space-y-4">
                            <h3 className="text-xl font-bold text-white flex items-center gap-2">
                                <span className="w-3 h-3 rounded-full bg-[#00F2FE]" />
                                The Tech in Soul Solution
                            </h3>
                            <p className="text-sm text-slate-300 leading-relaxed">
                                {project.solution || project.description}
                            </p>
                        </div>

                        <div className="glass-card rounded-2xl p-8 space-y-4">
                            <h3 className="text-xl font-bold text-white flex items-center gap-2">
                                <span className="w-3 h-3 rounded-full bg-emerald-400" />
                                Key Business Results
                            </h3>
                            <p className="text-sm text-slate-300 leading-relaxed">
                                {project.result || 'Achieved remarkable improvements in performance, scalability, and conversion metrics.'}
                            </p>
                        </div>
                    </div>

                    {/* Testimonial callout */}
                    {project.testimonial && (
                        <div className="glass-card rounded-2xl p-8 relative border border-[#00F2FE]/30 bg-[#0B132B]">
                            <Quote className="w-10 h-10 text-[#00F2FE]/20 absolute top-4 right-4" />
                            <p className="text-base text-slate-200 italic mb-4">
                                "{project.testimonial}"
                            </p>
                            <span className="text-xs font-bold text-[#00F2FE]">— {project.client_name} Leadership</span>
                        </div>
                    )}

                    {/* Related Projects */}
                    {relatedProjects && relatedProjects.length > 0 && (
                        <div className="pt-12 border-t border-slate-800 space-y-6">
                            <h3 className="text-2xl font-bold text-white">More Case Studies</h3>
                            <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
                                {relatedProjects.map((p) => (
                                    <ProjectCard key={p.id} project={p} />
                                ))}
                            </div>
                        </div>
                    )}
                </div>
            </main>

            <PublicFooter />
        </div>
    );
}
