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 PointOfInterestCard from '@/components/point-of-interest-card';
import AiTouristGuide from '@/components/ai-tourist-guide';
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import { Card, CardContent } from '@/components/ui/card';
import { 
    Pagination,
    PaginationContent,
    PaginationEllipsis,
    PaginationItem,
    PaginationLink,
    PaginationNext,
    PaginationPrevious,
} from '@/components/ui/pagination';
import { Input } from '@/components/ui/input';
import { Search, Filter, MapPin, Star, Clock, Users, TrendingUp, Globe, Sparkles } from 'lucide-react';

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

interface PaginationData {
    current_page: number;
    data: PointOfInterest[];
    first_page_url: string;
    from: number;
    last_page: number;
    last_page_url: string;
    links: Array<{
        url: string | null;
        label: string;
        active: boolean;
    }>;
    next_page_url: string | null;
    path: string;
    per_page: number;
    prev_page_url: string | null;
    to: number;
    total: number;
}

interface Props extends SharedData {
    pointsOfInterest: PaginationData;
}

const breadcrumbs: BreadcrumbItem[] = [
    {
        title: 'Home',
        href: '/',
    },
    {
        title: 'Points of Interest',
        href: '/points-of-interest',
    },
];

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

    const generatePaginationRange = () => {
        const current = pointsOfInterest.current_page;
        const last = pointsOfInterest.last_page;
        const range: number[] = [];
        
        if (last <= 7) {
            for (let i = 1; i <= last; i++) {
                range.push(i);
            }
        } else {
            if (current <= 4) {
                for (let i = 1; i <= 5; i++) {
                    range.push(i);
                }
                range.push(-1, last);
            } else if (current >= last - 3) {
                range.push(1, -1);
                for (let i = last - 4; i <= last; i++) {
                    range.push(i);
                }
            } else {
                range.push(1, -1, current - 1, current, current + 1, -1, last);
            }
        }
        
        return range;
    };

    return (
        <AppLayout breadcrumbs={breadcrumbs}>
            <Head title="Points of Interest - Smartsense" />
            
            <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">
                {/* Simplified Header - Focus on Search & Action */}
                <section className="px-4 py-8 mx-auto max-w-[1400px] w-full">
                    <div className="text-center mb-8">
                        <h1 className="text-2xl lg:text-3xl font-bold text-foreground mb-4">
                            {t('Find Amazing Places to Visit')}
                        </h1>
                        
                        {/* Direct Search Bar */}
                        <div className="max-w-2xl mx-auto">
                            <div className="relative">
                                <div className="absolute inset-y-0 left-0 flex items-center pl-4">
                                    <Search className="h-5 w-5 text-muted-foreground" />
                                </div>
                                <Input
                                    type="text"
                                    placeholder={t('Search museums, restaurants, attractions...')}
                                    className="pl-12 pr-32 h-12 text-base rounded-2xl border-0 shadow-lg bg-white/80 backdrop-blur-sm focus:ring-2 focus:ring-blue-500/20"
                                />
                                <div className="absolute inset-y-0 right-0 flex items-center pr-2">
                                    <Button size="sm" className="bg-gradient-to-r from-blue-600 to-purple-600 hover:from-blue-700 hover:to-purple-700 rounded-xl px-6">
                                        <Search className="h-4 w-4 mr-2" />
                                        {t('Search')}
                                    </Button>
                                </div>
                            </div>
                        </div>
                    </div>
                </section>

                {/* Quick Filters */}
                <section className="px-4 mx-auto max-w-[1400px] w-full mb-6">
                    <div className="flex flex-wrap items-center justify-between gap-3">
                        <div className="flex flex-wrap gap-2">
                            <Button variant="outline" size="sm" className="gap-2">
                                <Filter className="h-4 w-4" />
                                {t('All')}
                            </Button>
                            <Button variant="ghost" size="sm">{t('Museums')}</Button>
                            <Button variant="ghost" size="sm">{t('Restaurants')}</Button>
                            <Button variant="ghost" size="sm">{t('Landmarks')}</Button>
                        </div>
                        <span className="text-sm text-muted-foreground">
                            {pointsOfInterest.total} {t('places')}
                        </span>
                    </div>
                </section>

                {/* Points of Interest Grid */}
                <section className="px-4 mx-auto max-w-[1400px] w-full pb-16 lg:pb-24">
                    {pointsOfInterest.data.length > 0 ? (
                        <>
                            <div className="grid gap-6 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4">
                                {pointsOfInterest.data.map((poi) => (
                                    <Link 
                                        key={poi.id} 
                                        href={`/points-of-interest/${poi.id}`}
                                        className="block group"
                                    >
                                        <div className="transform transition-all duration-300 group-hover:scale-105 group-hover:shadow-xl">
                                            <PointOfInterestCard
                                                image={poi.image_url || 'https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=400&h=300&fit=crop&crop=center'}
                                                title={poi.name}
                                                description={poi.description}
                                                category={poi.category?.name_en || 'Uncategorized'}
                                                rating={4.5}
                                                priceRange={t('Free')}
                                                openingHours={t('9:00 AM - 6:00 PM')}
                                                location={t('City Center')}
                                                features={[]}
                                                className="border-0 shadow-lg bg-white/80 backdrop-blur-sm hover:bg-white/90"
                                            />
                                        </div>
                                    </Link>
                                ))}
                            </div>

                            {/* Pagination */}
                            {pointsOfInterest.last_page > 1 && (
                                <div className="flex justify-center mt-16">
                                    <Pagination>
                                        <PaginationContent className="bg-white/60 backdrop-blur-sm border-0 shadow-lg rounded-lg p-1">
                                            {pointsOfInterest.prev_page_url && (
                                                <PaginationItem>
                                                    <PaginationPrevious 
                                                        href={pointsOfInterest.prev_page_url}
                                                        className="hover:bg-blue-50 dark:hover:bg-blue-900/20"
                                                    />
                                                </PaginationItem>
                                            )}

                                            {generatePaginationRange().map((page, index) => (
                                                <PaginationItem key={index}>
                                                    {page === -1 ? (
                                                        <PaginationEllipsis />
                                                    ) : (
                                                        <PaginationLink
                                                            href={`${pointsOfInterest.path}?page=${page}`}
                                                            isActive={page === pointsOfInterest.current_page}
                                                            className={page === pointsOfInterest.current_page 
                                                                ? "bg-gradient-to-r from-blue-600 to-purple-600 text-white hover:from-blue-700 hover:to-purple-700" 
                                                                : "hover:bg-blue-50 dark:hover:bg-blue-900/20"
                                                            }
                                                        >
                                                            {page}
                                                        </PaginationLink>
                                                    )}
                                                </PaginationItem>
                                            ))}

                                            {pointsOfInterest.next_page_url && (
                                                <PaginationItem>
                                                    <PaginationNext 
                                                        href={pointsOfInterest.next_page_url}
                                                        className="hover:bg-blue-50 dark:hover:bg-blue-900/20"
                                                    />
                                                </PaginationItem>
                                            )}
                                        </PaginationContent>
                                    </Pagination>
                                </div>
                            )}

                            {/* Enhanced Results Info */}
                            <div className="text-center mt-8">
                                <div className="inline-flex items-center gap-2 bg-white/60 dark:bg-background/60 backdrop-blur-sm rounded-full px-6 py-3 border border-gray-200/50 dark:border-gray-800/50 shadow-lg">
                                    <Sparkles className="h-4 w-4 text-blue-600" />
                                    <span className="text-sm text-muted-foreground">
                                        {t('Showing')} <span className="font-semibold text-foreground">{pointsOfInterest.from}-{pointsOfInterest.to}</span> {t('of')} <span className="font-semibold text-foreground">{pointsOfInterest.total}</span> {t('amazing destinations')}
                                    </span>
                                </div>
                            </div>
                        </>
                    ) : (
                        <div className="text-center py-20">
                            <Card className="max-w-md mx-auto border-0 shadow-xl bg-white/80 backdrop-blur-sm">
                                <CardContent className="p-12">
                                    <div className="mx-auto w-20 h-20 rounded-2xl bg-gradient-to-br from-blue-100 to-purple-100 dark:from-blue-900/20 dark:to-purple-900/20 flex items-center justify-center mb-6">
                                        <Search className="h-10 w-10 text-blue-600 dark:text-blue-400" />
                                    </div>
                                    <h3 className="text-xl font-bold text-foreground mb-3">
                                        {t('No destinations found')}
                                    </h3>
                                    <p className="text-muted-foreground mb-8 leading-relaxed">
                                        {t('We couldn\'t find any points of interest matching your criteria. Try adjusting your search or explore different categories.')}
                                    </p>
                                    <div className="space-y-3">
                                        <Button className="w-full bg-gradient-to-r from-blue-600 to-purple-600 hover:from-blue-700 hover:to-purple-700" asChild>
                                            <Link href="/">
                                                <Globe className="h-4 w-4 mr-2" />
                                                {t('Explore All Destinations')}
                                            </Link>
                                        </Button>
                                        <Button variant="outline" className="w-full" asChild>
                                            <Link href="/">
                                                {t('Back to Home')}
                                            </Link>
                                        </Button>
                                    </div>
                                </CardContent>
                            </Card>
                        </div>
                    )}
                </section>


                {/* AI Tourist Guide */}
                <AiTouristGuide />
            </div>
        </AppLayout>
    );
}