import { ArrowLeft, Search } from 'lucide-react';

import { Head, Link } from '@inertiajs/react';

import { Badge } from '@/Components/ui/badge';
import {
  Breadcrumb,
  BreadcrumbItem,
  BreadcrumbLink,
  BreadcrumbList,
  BreadcrumbPage,
  BreadcrumbSeparator,
} from '@/Components/ui/breadcrumb';
import { Button } from '@/Components/ui/button';
import { Card, CardContent, CardHeader, CardTitle } from '@/Components/ui/card';
import AdminLayout from '@/Layouts/AdminLayout';
import { getAdminStatusVariant } from '@/lib/adminStatusBadge';
import { formatDateTime } from '@/lib/format';

interface SerpSnapshotDetail {
  id: number;
  site_id: number;
  site_name: string | null;
  site_domain: string | null;
  keyword: string;
  target_url: string | null;
  status: string;
  language_code: string | null;
  location_code: number | null;
  error: string | null;
  result_count: number;
  nlp_term_count: number;
  top_nlp_terms: unknown[];
  fetched_at: string | null;
  expires_at: string | null;
  created_at: string;
}

interface Props {
  snapshot: SerpSnapshotDetail;
}

export default function AdminSerpSnapshotShow({ snapshot }: Props) {
  return (
    <AdminLayout>
      <Head title={`Admin - SERP Snapshot #${snapshot.id}`} />

      <div className="container py-6 space-y-6">
        <Breadcrumb>
          <BreadcrumbList>
            <BreadcrumbItem>
              <BreadcrumbLink asChild>
                <Link href="/admin">Admin</Link>
              </BreadcrumbLink>
            </BreadcrumbItem>
            <BreadcrumbSeparator />
            <BreadcrumbItem>
              <BreadcrumbLink asChild>
                <Link href={route('admin.serp-snapshots.index')}>SERP Snapshots</Link>
              </BreadcrumbLink>
            </BreadcrumbItem>
            <BreadcrumbSeparator />
            <BreadcrumbItem>
              <BreadcrumbPage>#{snapshot.id}</BreadcrumbPage>
            </BreadcrumbItem>
          </BreadcrumbList>
        </Breadcrumb>

        <div className="flex items-center gap-3">
          <Search className="h-5 w-5 text-muted-foreground" />
          <div>
            <div className="flex items-center gap-2">
              <h1 className="text-xl font-semibold">SERP Snapshot #{snapshot.id}</h1>
              <Badge variant={getAdminStatusVariant(snapshot.status)}>{snapshot.status}</Badge>
            </div>
            <p className="text-sm font-medium">{snapshot.keyword}</p>
          </div>
        </div>

        <div className="grid gap-6 md:grid-cols-2">
          <Card>
            <CardHeader>
              <CardTitle className="text-sm font-medium">Details</CardTitle>
            </CardHeader>
            <CardContent className="space-y-3 text-sm">
              <div>
                <p className="text-xs text-muted-foreground">Site</p>
                <Link href={route('admin.sites.show', { site: snapshot.site_id })} className="text-primary hover:underline font-medium">
                  {snapshot.site_name ?? '—'}
                </Link>
                {snapshot.site_domain && (
                  <p className="text-xs text-muted-foreground">{snapshot.site_domain}</p>
                )}
              </div>
              {snapshot.target_url && (
                <div>
                  <p className="text-xs text-muted-foreground">Target URL</p>
                  <p className="font-mono text-xs truncate">{snapshot.target_url}</p>
                </div>
              )}
              <div>
                <p className="text-xs text-muted-foreground">Language</p>
                <p className="font-mono">{snapshot.language_code ?? '—'}</p>
              </div>
              {snapshot.location_code != null && (
                <div>
                  <p className="text-xs text-muted-foreground">Location Code</p>
                  <p className="font-mono">{snapshot.location_code}</p>
                </div>
              )}
              <div>
                <p className="text-xs text-muted-foreground">Created</p>
                <p>{formatDateTime(snapshot.created_at)}</p>
              </div>
              <div>
                <p className="text-xs text-muted-foreground">Fetched</p>
                <p>{formatDateTime(snapshot.fetched_at) || '—'}</p>
              </div>
              <div>
                <p className="text-xs text-muted-foreground">Expires</p>
                <p>{formatDateTime(snapshot.expires_at) || '—'}</p>
              </div>
            </CardContent>
          </Card>

          <Card>
            <CardHeader>
              <CardTitle className="text-sm font-medium">Data Summary</CardTitle>
            </CardHeader>
            <CardContent className="space-y-3 text-sm">
              <div className="flex justify-between">
                <span className="text-muted-foreground">SERP Results</span>
                <span className="font-mono font-medium">{snapshot.result_count}</span>
              </div>
              <div className="flex justify-between">
                <span className="text-muted-foreground">NLP Terms</span>
                <span className="font-mono font-medium">{snapshot.nlp_term_count}</span>
              </div>
            </CardContent>
          </Card>

          {snapshot.error && (
            <Card className="md:col-span-2 border-destructive/50">
              <CardHeader>
                <CardTitle className="text-sm font-medium text-destructive">Error</CardTitle>
              </CardHeader>
              <CardContent>
                <pre className="text-xs bg-muted p-3 rounded-md overflow-auto whitespace-pre-wrap break-all">
                  {snapshot.error}
                </pre>
              </CardContent>
            </Card>
          )}

          {snapshot.top_nlp_terms.length > 0 && (
            <Card className="md:col-span-2">
              <CardHeader>
                <CardTitle className="text-sm font-medium">
                  Top NLP Terms (first 20 of {snapshot.nlp_term_count})
                </CardTitle>
              </CardHeader>
              <CardContent>
                <pre className="text-xs bg-muted p-3 rounded-md overflow-auto max-h-48">
                  {JSON.stringify(snapshot.top_nlp_terms, null, 2)}
                </pre>
              </CardContent>
            </Card>
          )}
        </div>

        <Button variant="outline" asChild>
          <Link href={route('admin.serp-snapshots.index')}>
            <ArrowLeft className="mr-2 h-4 w-4" />
            Back to SERP Snapshots
          </Link>
        </Button>
      </div>
    </AdminLayout>
  );
}
