import AppLayout from '@admin/layouts/app-layout';
import { Badge } from '@admin/components/ui/badge';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@admin/components/ui/card';
import { Input } from '@admin/components/ui/input';
import { Head, Link } from '@inertiajs/react';
import { BookOpen, FileText, Clock, Search, ArrowRight } from 'lucide-react';
import { ReactNode, useState } from 'react';
import HeadingSmall from '@admin/components/heading-small';

interface DocFile {
    name: string;
    title: string;
    path: string;
    size: number;
    modified: number;
}

interface DocsIndexProps {
    files: DocFile[];
}

function Index({ files }: DocsIndexProps) {
    const [searchQuery, setSearchQuery] = useState('');

    const formatDate = (timestamp: number) => {
        return new Date(timestamp * 1000).toLocaleDateString('en-US', {
            year: 'numeric',
            month: 'short',
            day: 'numeric',
        });
    };

    const formatSize = (bytes: number) => {
        if (bytes < 1024) return bytes + ' B';
        if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB';
        return (bytes / (1024 * 1024)).toFixed(1) + ' MB';
    };

    // Filter files based on search query
    const filteredFiles = files.filter((file) =>
        file.title.toLowerCase().includes(searchQuery.toLowerCase()) ||
        file.name.toLowerCase().includes(searchQuery.toLowerCase())
    );

    return (
        <>
            <Head title="Documentation" />
            <div className="space-y-6 p-4">
                <Card className="rounded-lg border px-5 py-3">
                    <div className="flex w-full items-start justify-between">
                        <HeadingSmall
                            title="Documentation"
                            description="Browse and search through developer documentation and guides"
                        />
                        {files.length > 0 && (
                            <Badge className="px-3 py-1 text-white">
                                {files.length} {files.length === 1 ? 'Document' : 'Documents'}
                            </Badge>
                        )}
                    </div>
                    <CardContent className="px-0 py-4">

                {/* Search Bar */}
                {files.length > 0 && (
                    <div className="relative mb-4">
                        <Search className="absolute top-3 left-3 h-4 w-4 text-muted-foreground" />
                        <Input
                            placeholder="Search documentation..."
                            value={searchQuery}
                            onChange={(e) => setSearchQuery(e.target.value)}
                            className="pl-10"
                        />
                    </div>
                )}

                {/* Documentation Files */}
                {files.length === 0 ? (
                    <Card className="border-dashed">
                        <CardContent className="flex flex-col items-center justify-center py-16">
                            <div className="mb-4 flex h-16 w-16 items-center justify-center rounded-full bg-muted">
                                <BookOpen className="h-8 w-8 text-muted-foreground" />
                            </div>
                            <h3 className="mb-2 text-xl font-semibold">No Documentation Found</h3>
                            <p className="mb-4 text-center text-sm text-muted-foreground">
                                Add markdown files to the <code className="rounded bg-muted px-2 py-1">/docs</code> directory
                            </p>
                            <Badge variant="outline">Getting Started</Badge>
                        </CardContent>
                    </Card>
                ) : filteredFiles.length === 0 ? (
                    <Card className="border-dashed">
                        <CardContent className="flex flex-col items-center justify-center py-16">
                            <Search className="mb-4 h-12 w-12 text-muted-foreground" />
                            <h3 className="mb-2 text-lg font-semibold">No Results Found</h3>
                            <p className="text-sm text-muted-foreground">
                                Try adjusting your search query
                            </p>
                        </CardContent>
                    </Card>
                ) : (
                    <div className="grid gap-3 md:grid-cols-2 lg:grid-cols-4">
                        {filteredFiles.map((file) => (
                            <Link key={file.name} href={route('admin.developer.docs.show', file.path)}>
                                <Card className="group p-4 transition-all hover:shadow-md hover:border-primary/50">
                                    <div className="flex items-start gap-3">
                                        <div className="flex h-10 w-10 shrink-0 items-center justify-center rounded-lg bg-primary/10">
                                            <FileText className="h-5 w-5 text-primary" />
                                        </div>
                                        <div className="min-w-0 flex-1">
                                            <h3 className="font-semibold text-sm line-clamp-1 mb-1">{file.title}</h3>
                                            <div className="flex items-center gap-2 text-xs text-muted-foreground">
                                                <Badge className="text-[9px] font-mono text-white px-1.5 py-0">
                                                    .md
                                                </Badge>
                                                <span className="truncate">{formatSize(file.size)}</span>
                                            </div>
                                        </div>
                                    </div>
                                </Card>
                            </Link>
                        ))}
                    </div>
                )}
                    </CardContent>
                </Card>
            </div>
        </>
    );
}

Index.layout = (page: ReactNode) => (
    <AppLayout
        breadcrumbs={[
            { title: 'Home', href: '/' },
            { title: 'Developer', href: '#' },
            { title: 'Docs', href: '#' },
        ]}
        title="Documentation"
    >
        {page}
    </AppLayout>
);

export default Index;
