import { type BreadcrumbItem, type SharedData } from '@/types';
import { Head, Link, usePage } from '@inertiajs/react';
import { useLaravelReactI18n } from 'laravel-react-i18n';
import AppLayout from '@/layouts/app-layout';
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import { Card, CardContent } from '@/components/ui/card';
import AiTouristGuide from '@/components/ai-tourist-guide';
import { cn } from '@/lib/utils';
import { 
    ArrowLeft, 
    MapPin, 
    Clock, 
    Star, 
    User,
    Calendar,
    Heart,
    Share2,
    Camera,
    Wifi,
    Car,
    Coffee,
    Shield,
    Info,
    Phone,
    Globe,
    DollarSign,
    Users,
    ChevronLeft
} from 'lucide-react';

interface PointOfInterest {
    id: number;
    name: string;
    description: string;
    image_url: string;
    created_at: string;
    updated_at: string;
    category: {
        id: number;
        name_en: string;
        name_ro: string;
        image_url?: string;
    };
    created_by_user: {
        id: number;
        name: string;
    };
}

interface Props extends SharedData {
    pointOfInterest: PointOfInterest;
}

export default function Show() {
    const { pointOfInterest } = usePage<Props>().props;
    const { t } = useLaravelReactI18n();

    const breadcrumbs: BreadcrumbItem[] = [
        {
            title: 'Home',
            href: '/',
        },
        {
            title: 'Points of Interest',
            href: '/points-of-interest',
        },
        {
            title: pointOfInterest.name,
            href: `/points-of-interest/${pointOfInterest.id}`,
        },
    ];

    const renderStars = (rating: number) => {
        return Array.from({ length: 5 }, (_, i) => (
            <Star
                key={i}
                className={cn(
                    "w-5 h-5",
                    i < rating ? "text-yellow-400 fill-current" : "text-gray-300"
                )}
            />
        ));
    };

    const formatDate = (dateString: string) => {
        return new Date(dateString).toLocaleDateString('en-US', {
            year: 'numeric',
            month: 'long',
            day: 'numeric'
        });
    };

    return (
        <AppLayout breadcrumbs={breadcrumbs}>
            <Head title={`${pointOfInterest.name} - Points of Interest`} />
            
            <div className="flex flex-col min-h-screen bg-gradient-to-b from-blue-50/30 via-white to-white dark:from-blue-950/20 dark:via-background dark:to-background">

                {/* Hero Section */}
                <section className="px-4 pt-6 lg:pt-8 mx-auto max-w-[1400px] w-full">
                    <div className="grid gap-6 lg:gap-12 lg:grid-cols-12">
                        {/* Image Gallery */}
                        <div className="lg:col-span-7">
                            <div className="relative aspect-[4/3] lg:aspect-[16/10] overflow-hidden rounded-2xl lg:rounded-3xl shadow-2xl">
                                <img 
                                    src={pointOfInterest.image_url || 'https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=1200&h=800&fit=crop&crop=center'}
                                    alt={pointOfInterest.name}
                                    className="w-full h-full object-cover hover:scale-105 transition-transform duration-700"
                                    onError={(e) => {
                                        const target = e.target as HTMLImageElement;
                                        target.src = 'https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=1200&h=800&fit=crop&crop=center';
                                    }}
                                />
                                <div className="absolute inset-0 bg-gradient-to-t from-black/30 via-transparent to-transparent" />
                                
                                {/* Action Buttons Overlay */}
                                <div className="absolute top-4 right-4 flex gap-2">
                                    <Button size="sm" variant="outline" className="bg-white/95 backdrop-blur-sm border-0 shadow-lg hover:bg-white">
                                        <Heart className="h-4 w-4" />
                                    </Button>
                                    <Button size="sm" variant="outline" className="bg-white/95 backdrop-blur-sm border-0 shadow-lg hover:bg-white">
                                        <Share2 className="h-4 w-4" />
                                    </Button>
                                </div>
                                
                                {/* Category Badge */}
                                <Badge className="absolute top-4 left-4 bg-gradient-to-r from-blue-600 to-purple-600 text-white shadow-lg backdrop-blur-sm border-0 px-3 py-1.5 text-sm font-medium">
                                    {pointOfInterest.category?.name_en || 'Uncategorized'}
                                </Badge>
                                
                                {/* Photo Count */}
                                <div className="absolute bottom-4 right-4 bg-black/70 text-white px-3 py-1.5 rounded-full text-sm font-medium backdrop-blur-sm">
                                    <Camera className="h-4 w-4 inline mr-1" />
                                    {Math.floor(Math.random() * 50) + 10} photos
                                </div>
                            </div>
                        </div>

                        {/* Content */}
                        <div className="lg:col-span-5 flex flex-col justify-center space-y-6">
                            <div className="space-y-4">
                                <div className="flex items-start justify-between gap-4">
                                    <h1 className="text-2xl sm:text-3xl lg:text-4xl font-bold text-foreground leading-tight">
                                        {pointOfInterest.name}
                                    </h1>
                                    <div className="hidden lg:flex gap-2 shrink-0">
                                        <Button size="sm" variant="outline" className="gap-2">
                                            <Heart className="h-4 w-4" />
                                            {t('Save')}
                                        </Button>
                                        <Button size="sm" variant="outline" className="gap-2">
                                            <Share2 className="h-4 w-4" />
                                            {t('Share')}
                                        </Button>
                                    </div>
                                </div>
                                
                                {/* Rating & Location */}
                                <div className="flex flex-col sm:flex-row sm:items-center gap-3">
                                    <div className="flex items-center gap-2">
                                        <div className="flex items-center gap-1">
                                            {renderStars(4)}
                                            <span className="ml-1 font-semibold text-foreground">4.0</span>
                                        </div>
                                        <span className="text-sm text-muted-foreground">({Math.floor(Math.random() * 200) + 50} {t('reviews')})</span>
                                    </div>
                                    <div className="flex items-center gap-2 text-muted-foreground">
                                        <MapPin className="h-4 w-4" />
                                        <span className="text-sm font-medium">{t('City Center, Downtown')}</span>
                                    </div>
                                </div>

                                <p className="text-muted-foreground leading-relaxed">
                                    {pointOfInterest.description}
                                </p>
                            </div>

                            {/* Quick Info Cards */}
                            <div className="grid gap-3 sm:grid-cols-2">
                                <Card className="border-0 shadow-md hover:shadow-lg transition-shadow duration-200">
                                    <CardContent className="p-4">
                                        <div className="flex items-center gap-3">
                                            <div className="p-2.5 rounded-xl bg-gradient-to-br from-blue-500 to-purple-600">
                                                <Clock className="h-5 w-5 text-white" />
                                            </div>
                                            <div className="flex-1">
                                                <p className="font-semibold text-foreground text-sm">{t('Open Now')}</p>
                                                <p className="text-sm text-muted-foreground">9:00 AM - 6:00 PM</p>
                                            </div>
                                        </div>
                                    </CardContent>
                                </Card>

                                <Card className="border-0 shadow-md hover:shadow-lg transition-shadow duration-200">
                                    <CardContent className="p-4">
                                        <div className="flex items-center gap-3">
                                            <div className="p-2.5 rounded-xl bg-gradient-to-br from-green-500 to-emerald-600">
                                                <DollarSign className="h-5 w-5 text-white" />
                                            </div>
                                            <div className="flex-1">
                                                <p className="font-semibold text-foreground text-sm">{t('Free Entry')}</p>
                                                <p className="text-sm text-muted-foreground">{t('No booking required')}</p>
                                            </div>
                                        </div>
                                    </CardContent>
                                </Card>
                            </div>

                            {/* Action Buttons */}
                            <div className="flex flex-col sm:flex-row gap-3 pt-2">
                                <Button size="lg" className="flex-1 bg-gradient-to-r from-blue-600 to-purple-600 hover:from-blue-700 hover:to-purple-700 gap-2">
                                    <Phone className="h-4 w-4" />
                                    {t('Contact Guide')}
                                </Button>
                                <Button variant="outline" size="lg" className="flex-1 gap-2">
                                    <Info className="h-4 w-4" />
                                    {t('Get Directions')}
                                </Button>
                            </div>
                        </div>
                    </div>
                </section>

                {/* Features Overview - Mobile Optimized */}
                <section className="px-4 py-8 mx-auto max-w-[1400px] w-full">
                    <div className="bg-white/60 dark:bg-background/60 backdrop-blur-sm rounded-2xl border border-gray-200/50 dark:border-gray-800/50 p-6 shadow-lg">
                        <h2 className="text-lg font-bold text-foreground mb-4 flex items-center gap-2">
                            <Wifi className="h-5 w-5 text-blue-600" />
                            {t('Available Amenities')}
                        </h2>
                        <div className="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-6 gap-4">
                            {[
                                { icon: Wifi, label: 'Free WiFi', available: true },
                                { icon: Car, label: 'Parking', available: true },
                                { icon: Coffee, label: 'Café', available: true },
                                { icon: Shield, label: 'Security', available: true },
                                { icon: Users, label: 'Group Tours', available: false },
                                { icon: Globe, label: 'Multi-language', available: true }
                            ].map((amenity, index) => {
                                const Icon = amenity.icon;
                                return (
                                    <div key={index} className={cn(
                                        "flex flex-col items-center gap-2 p-3 rounded-xl border text-center transition-colors",
                                        amenity.available 
                                            ? "bg-green-50 border-green-200 text-green-700 dark:bg-green-950/20 dark:border-green-800/30 dark:text-green-400"
                                            : "bg-gray-50 border-gray-200 text-gray-400 dark:bg-gray-950/20 dark:border-gray-800/30 dark:text-gray-500"
                                    )}>
                                        <Icon className="h-5 w-5" />
                                        <span className="text-xs font-medium">{t(amenity.label)}</span>
                                    </div>
                                );
                            })}
                        </div>
                    </div>
                </section>

                {/* Details Section */}
                <section className="px-4 py-8 lg:py-16 mx-auto max-w-[1400px] w-full">
                    <div className="grid gap-8 lg:grid-cols-3">
                        {/* Main Content */}
                        <div className="lg:col-span-2 space-y-6 lg:space-y-8">
                            {/* About */}
                            <Card className="border-0 shadow-lg">
                                <CardContent className="p-6 lg:p-8">
                                    <h2 className="text-xl lg:text-2xl font-bold mb-6 flex items-center gap-3">
                                        <div className="p-2 rounded-xl bg-gradient-to-br from-blue-500 to-purple-600">
                                            <Info className="h-5 w-5 text-white" />
                                        </div>
                                        {t('About This Place')}
                                    </h2>
                                    <div className="prose max-w-none text-muted-foreground">
                                        <p className="leading-relaxed mb-4 text-base">
                                            {pointOfInterest.description}
                                        </p>
                                        <p className="leading-relaxed text-base">
                                            {t('This remarkable destination offers visitors an authentic glimpse into the local culture and heritage. Whether you\'re a history enthusiast, culture lover, or simply looking to explore something new, this location provides a memorable experience for travelers of all backgrounds and interests.')}
                                        </p>
                                    </div>
                                </CardContent>
                            </Card>

                            {/* Experience Highlights */}
                            <Card className="border-0 shadow-lg">
                                <CardContent className="p-6 lg:p-8">
                                    <h3 className="text-xl font-bold mb-6 flex items-center gap-3">
                                        <div className="p-2 rounded-xl bg-gradient-to-br from-green-500 to-emerald-600">
                                            <Star className="h-5 w-5 text-white" />
                                        </div>
                                        {t('Experience Highlights')}
                                    </h3>
                                    <div className="grid gap-4 sm:grid-cols-2">
                                        {[
                                            { icon: '🎯', title: 'Expert Local Guide', desc: 'Professional guidance throughout your visit' },
                                            { icon: '🎫', title: 'All Entrance Fees', desc: 'No hidden costs or surprise charges' },
                                            { icon: '📸', title: 'Photo Opportunities', desc: 'Capture memories at the best spots' },
                                            { icon: '🌟', title: 'Insider Knowledge', desc: 'Learn stories only locals know' },
                                            { icon: '🛡️', title: 'Safe Environment', desc: 'Secure and comfortable experience' },
                                            { icon: '♿', title: 'Accessible Options', desc: 'Accommodating for all visitors' }
                                        ].map((highlight, index) => (
                                            <div key={index} className="flex gap-4 p-4 rounded-xl bg-gradient-to-br from-gray-50 to-gray-100/50 dark:from-gray-900/20 dark:to-gray-800/20 border border-gray-200/50 dark:border-gray-700/50">
                                                <div className="text-2xl">{highlight.icon}</div>
                                                <div className="flex-1">
                                                    <h4 className="font-semibold text-foreground text-sm">{t(highlight.title)}</h4>
                                                    <p className="text-xs text-muted-foreground mt-1">{t(highlight.desc)}</p>
                                                </div>
                                            </div>
                                        ))}
                                    </div>
                                </CardContent>
                            </Card>
                        </div>

                        {/* Sidebar */}
                        <div className="space-y-6">
                            {/* Essential Info */}
                            <Card className="border-0 shadow-lg">
                                <CardContent className="p-6">
                                    <h3 className="text-lg font-bold mb-6 text-center">{t('Visit Information')}</h3>
                                    
                                    {/* Opening Hours */}
                                    <div className="space-y-4 mb-6">
                                        <div className="flex items-start gap-3">
                                            <div className="p-2 rounded-lg bg-blue-100 dark:bg-blue-900/20">
                                                <Clock className="h-4 w-4 text-blue-600 dark:text-blue-400" />
                                            </div>
                                            <div className="flex-1">
                                                <p className="font-medium text-foreground text-sm">{t('Opening Hours')}</p>
                                                <p className="text-sm text-muted-foreground mt-1">{t('Daily: 9:00 AM - 6:00 PM')}</p>
                                                <p className="text-xs text-green-600 dark:text-green-400 font-medium">{t('Open Now')}</p>
                                            </div>
                                        </div>
                                        
                                        <div className="flex items-start gap-3">
                                            <div className="p-2 rounded-lg bg-green-100 dark:bg-green-900/20">
                                                <MapPin className="h-4 w-4 text-green-600 dark:text-green-400" />
                                            </div>
                                            <div className="flex-1">
                                                <p className="font-medium text-foreground text-sm">{t('Location')}</p>
                                                <p className="text-sm text-muted-foreground mt-1">{t('City Center, Downtown Area')}</p>
                                                <Button variant="link" className="p-0 h-auto text-xs text-blue-600 hover:text-blue-700">
                                                    {t('Get Directions')}
                                                </Button>
                                            </div>
                                        </div>
                                        
                                        <div className="flex items-start gap-3">
                                            <div className="p-2 rounded-lg bg-purple-100 dark:bg-purple-900/20">
                                                <Phone className="h-4 w-4 text-purple-600 dark:text-purple-400" />
                                            </div>
                                            <div className="flex-1">
                                                <p className="font-medium text-foreground text-sm">{t('Contact')}</p>
                                                <p className="text-sm text-muted-foreground mt-1">+1 (555) 123-4567</p>
                                                <Button variant="link" className="p-0 h-auto text-xs text-blue-600 hover:text-blue-700">
                                                    {t('Call Now')}
                                                </Button>
                                            </div>
                                        </div>
                                    </div>

                                    {/* CTA Button */}
                                    <Button className="w-full bg-gradient-to-r from-blue-600 to-purple-600 hover:from-blue-700 hover:to-purple-700 mb-4">
                                        {t('Plan Your Visit')}
                                    </Button>
                                </CardContent>
                            </Card>

                            {/* Creator & Stats */}
                            <Card className="border-0 shadow-lg">
                                <CardContent className="p-6">
                                    <div className="space-y-4">
                                        {/* Creator */}
                                        <div>
                                            <h4 className="font-semibold text-foreground mb-3">{t('Local Expert')}</h4>
                                            <div className="flex items-center gap-3">
                                                <div className="w-10 h-10 rounded-full bg-gradient-to-br from-blue-500 to-purple-600 flex items-center justify-center">
                                                    <User className="h-5 w-5 text-white" />
                                                </div>
                                                <div>
                                                    <p className="font-medium text-foreground text-sm">
                                                        {pointOfInterest.created_by_user?.name || 'Anonymous'}
                                                    </p>
                                                    <p className="text-xs text-muted-foreground">{t('Tourism Guide')}</p>
                                                </div>
                                            </div>
                                        </div>

                                        {/* Quick Stats */}
                                        <div className="pt-4 border-t border-gray-200/50 dark:border-gray-700/50">
                                            <div className="grid grid-cols-2 gap-4 text-center">
                                                <div>
                                                    <p className="text-lg font-bold text-foreground">4.0</p>
                                                    <p className="text-xs text-muted-foreground">{t('Rating')}</p>
                                                </div>
                                                <div>
                                                    <p className="text-lg font-bold text-foreground">{Math.floor(Math.random() * 200) + 50}</p>
                                                    <p className="text-xs text-muted-foreground">{t('Reviews')}</p>
                                                </div>
                                            </div>
                                        </div>

                                        {/* Category */}
                                        <div className="pt-2">
                                            <Badge className="w-full justify-center bg-gradient-to-r from-gray-100 to-gray-200 dark:from-gray-800 dark:to-gray-700 text-gray-800 dark:text-gray-200 border-0">
                                                {pointOfInterest.category?.name_en || 'Uncategorized'}
                                            </Badge>
                                        </div>
                                    </div>
                                </CardContent>
                            </Card>
                        </div>
                    </div>
                </section>

                {/* Mobile Action Bar */}
                <div className="lg:hidden fixed bottom-0 left-0 right-0 bg-white dark:bg-background border-t border-gray-200 dark:border-gray-800 p-4 safe-area-pb z-50">
                    <div className="flex gap-3">
                        <Button variant="outline" className="flex-1 gap-2">
                            <Heart className="h-4 w-4" />
                            {t('Save')}
                        </Button>
                        <Button className="flex-1 bg-gradient-to-r from-blue-600 to-purple-600 hover:from-blue-700 hover:to-purple-700 gap-2">
                            <Phone className="h-4 w-4" />
                            {t('Contact')}
                        </Button>
                    </div>
                </div>

                {/* AI Tourist Guide */}
                <AiTouristGuide pointOfInterestName={pointOfInterest.name} />
            </div>
        </AppLayout>
    );
}