import AppLayout from '@admin/layouts/app-layout';
import { Badge } from '@admin/components/ui/badge';
import { Button } from '@admin/components/ui/button';
import { Card, CardContent } from '@admin/components/ui/card';
import { ScrollArea } from '@admin/components/ui/scroll-area';
import { Head, Link } from '@inertiajs/react';
import { ArrowLeft, BookOpen, FileText, ChevronRight } from 'lucide-react';
import { ReactNode } from 'react';
import ReactMarkdown from 'react-markdown';
import remarkGfm from 'remark-gfm';
import rehypeHighlight from 'rehype-highlight';
import 'highlight.js/styles/github-dark.css';

interface DocFile {
    name: string;
    title: string;
    path: string;
}

interface DocsShowProps {
    file: string;
    content: string;
    files: DocFile[];
    title: string;
}

function Show({ file, content, files, title }: DocsShowProps) {
    return (
        <>
            <Head title={title} />
            <div className="no-scrollbar rounded-xl p-2">
                <div className="">
                    {/* Header matching User List layout */}
                    <div className="mb-6 flex flex-col items-start justify-between gap-4 sm:flex-row sm:items-center">
                    <div className="grid grid-cols-1 gap-1">
                        <h2 className="text-xl font-bold sm:text-2xl">
                            {title}
                        </h2>
                        <div className="flex items-center text-sm text-gray-600">
                            <Link href={route('developer.docs.index')} className="hover:text-foreground transition-colors">
                                Dev Docs
                            </Link>
                            <span className="mx-2">›</span>
                            <span>{title}</span>
                        </div>
                    </div>
                    <div className="flex flex-wrap gap-2 sm:gap-4">
                        <Link href={route('developer.docs.index')}>
                            <Button variant="outline" size="sm">
                                <ArrowLeft className="mr-2 h-4 w-4" />
                                <span className="hidden sm:inline">Back</span>
                            </Button>
                        </Link>
                    </div>
                </div>

                <div className="grid gap-6 lg:grid-cols-4">
                    {/* Sidebar Navigation */}
                    <div className="lg:col-span-1">
                        <div className="sticky top-6">
                            <Card>
                                <CardContent className="p-4">
                                    <h3 className="mb-3 flex items-center gap-2 text-sm font-semibold">
                                        <BookOpen className="h-4 w-4 text-primary" />
                                        All Documents
                                    </h3>
                                    <ScrollArea className="h-[calc(100vh-16rem)]">
                                        <div className="space-y-1 pr-4">
                                            {files.map((doc) => (
                                                <Link
                                                    key={doc.name}
                                                    href={route('admin.developer.docs.show', doc.path)}
                                                    className={`flex items-center gap-2 rounded-md px-2 py-1.5 text-sm transition-colors ${
                                                        doc.name === file
                                                            ? 'bg-primary/10 text-primary font-medium'
                                                            : 'text-muted-foreground hover:bg-accent hover:text-foreground'
                                                    }`}
                                                >
                                                    <FileText className="h-3.5 w-3.5 shrink-0" />
                                                    <span className="truncate">{doc.title}</span>
                                                    {doc.name === file && (
                                                        <ChevronRight className="ml-auto h-3.5 w-3.5 shrink-0" />
                                                    )}
                                                </Link>
                                            ))}
                                        </div>
                                    </ScrollArea>
                                </CardContent>
                            </Card>
                        </div>
                    </div>

                    {/* Main Content */}
                    <div className="lg:col-span-3">
                        <Card>
                            <CardContent className="p-8">
                                <div className="prose prose-slate dark:prose-invert prose-headings:scroll-mt-20 prose-headings:font-bold prose-h1:text-3xl prose-h2:text-2xl prose-h2:border-b prose-h2:pb-2 prose-a:text-primary prose-a:no-underline hover:prose-a:underline prose-code:text-primary prose-pre:bg-slate-900 prose-pre:text-slate-50 max-w-none">
                                    <ReactMarkdown
                                        remarkPlugins={[remarkGfm]}
                                        rehypePlugins={[rehypeHighlight]}
                                        components={{
                                            // Custom styling for code blocks
                                            code: ({ node, inline, className, children, ...props }: any) => {
                                                return inline ? (
                                                    <code className="rounded bg-muted px-1.5 py-0.5 text-sm" {...props}>
                                                        {children}
                                                    </code>
                                                ) : (
                                                    <code className={className} {...props}>
                                                        {children}
                                                    </code>
                                                );
                                            },
                                            // Custom styling for tables
                                            table: ({ children, ...props }: any) => (
                                                <div className="my-6 overflow-x-auto">
                                                    <table className="w-full" {...props}>
                                                        {children}
                                                    </table>
                                                </div>
                                            ),
                                            // Custom styling for links
                                            a: ({ children, href, ...props }: any) => (
                                                <a
                                                    href={href}
                                                    className="text-primary hover:underline"
                                                    target={href?.startsWith('http') ? '_blank' : undefined}
                                                    rel={href?.startsWith('http') ? 'noopener noreferrer' : undefined}
                                                    {...props}
                                                >
                                                    {children}
                                                </a>
                                            ),
                                        }}
                                    >
                                        {content}
                                    </ReactMarkdown>
                                </div>
                            </CardContent>
                        </Card>
                    </div>
                </div>
                </div>
            </div>
        </>
    );
}

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

export default Show;
