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

import PageHeader from '@/Components/layout/PageHeader';
import {
  Breadcrumb,
  BreadcrumbItem,
  BreadcrumbLink,
  BreadcrumbList,
  BreadcrumbPage,
  BreadcrumbSeparator,
} from '@/Components/ui/breadcrumb';
import { Card, CardContent, CardHeader, CardTitle } from '@/Components/ui/card';
import AdminLayout from '@/Layouts/AdminLayout';
import { formatDate, formatRelativeTime, formatNumber } from '@/lib/format';

interface WpPostDetail {
  id: number;
  title: string;
  url: string;
  word_count: number | null;
  post_type: string;
  post_status: string;
  content: string | null;
  wp_post_id: number | null;
  wp_modified_at: string | null;
  created_at: string;
}

interface SiteInfo {
  id: number;
  name: string;
  domain: string;
  user: { id: number; name: string; email: string } | null;
}

interface Props {
  post: WpPostDetail;
  site: SiteInfo;
}

export default function WpPostsShow({ post, site }: Props) {
  return (
    <AdminLayout>
      <Head title={`WP Post — ${post.title}`} />
      <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="/admin/wp-posts">WP Posts</Link>
              </BreadcrumbLink>
            </BreadcrumbItem>
            <BreadcrumbSeparator />
            <BreadcrumbItem>
              <BreadcrumbPage className="max-w-xs truncate">{post.title}</BreadcrumbPage>
            </BreadcrumbItem>
          </BreadcrumbList>
        </Breadcrumb>

        <PageHeader title={post.title} subtitle={post.url} />

        <div className="grid gap-4 md:grid-cols-2">
          {/* Post Metadata */}
          <Card>
            <CardHeader>
              <CardTitle className="text-sm font-medium">Post Details</CardTitle>
            </CardHeader>
            <CardContent className="space-y-2 text-sm">
              <div className="flex justify-between">
                <span className="text-muted-foreground">WP Post ID</span>
                <span className="font-mono">{post.wp_post_id ?? '—'}</span>
              </div>
              <div className="flex justify-between">
                <span className="text-muted-foreground">Post Type</span>
                <span>{post.post_type}</span>
              </div>
              <div className="flex justify-between">
                <span className="text-muted-foreground">Status</span>
                <span>{post.post_status}</span>
              </div>
              <div className="flex justify-between">
                <span className="text-muted-foreground">Word Count</span>
                <span className="font-mono">{post.word_count != null ? formatNumber(post.word_count) : '—'}</span>
              </div>
              <div className="flex justify-between">
                <span className="text-muted-foreground">Last Modified</span>
                <span>{formatRelativeTime(post.wp_modified_at)}</span>
              </div>
              <div className="flex justify-between">
                <span className="text-muted-foreground">Synced</span>
                <span>{formatDate(post.created_at)}</span>
              </div>
            </CardContent>
          </Card>

          {/* Site Info */}
          <Card>
            <CardHeader>
              <CardTitle className="text-sm font-medium">Site</CardTitle>
            </CardHeader>
            <CardContent className="space-y-2 text-sm">
              <div className="flex justify-between">
                <span className="text-muted-foreground">Name</span>
                <Link
                  href={route('admin.sites.show', site.id)}
                  className="hover:underline font-medium"
                >
                  {site.name}
                </Link>
              </div>
              <div className="flex justify-between">
                <span className="text-muted-foreground">Domain</span>
                <span>{site.domain}</span>
              </div>
              {site.user && (
                <>
                  <div className="flex justify-between">
                    <span className="text-muted-foreground">Owner</span>
                    <Link
                      href={route('admin.users.show', site.user.id)}
                      className="hover:underline"
                    >
                      {site.user.name}
                    </Link>
                  </div>
                  <div className="flex justify-between">
                    <span className="text-muted-foreground">Email</span>
                    <span className="text-muted-foreground">{site.user.email}</span>
                  </div>
                </>
              )}
            </CardContent>
          </Card>
        </div>

        {/* Content Preview */}
        {post.content && (
          <Card>
            <CardHeader>
              <CardTitle className="text-sm font-medium">
                Content Preview{' '}
                <span className="text-xs text-muted-foreground font-normal">(first 5000 characters)</span>
              </CardTitle>
            </CardHeader>
            <CardContent>
              <pre className="whitespace-pre-wrap text-sm font-mono text-muted-foreground overflow-x-auto max-h-[500px] overflow-y-auto">
                {post.content}
              </pre>
            </CardContent>
          </Card>
        )}
      </div>
    </AdminLayout>
  );
}
