import { Head, Link } from '@inertiajs/react';
import { ArrowLeft, MessageSquare } from 'lucide-react';
import { AdminSurface, AdminSurfaceBar } from '@/components/admin-surface';
import { Badge } from '@/components/ui/badge';
import { Card } from '@/components/ui/card';
import AdminLayout from '@/layouts/admin-layout';
import { useT } from '@/lib/i18n';

type Message = {
    id: string;
    role: 'user' | 'assistant' | 'system' | string;
    content: string | null;
    created_at: string | null;
};

type Conversation = {
    id: string;
    agent: { id: string; name: string } | null;
    workspace: { id: string; name: string } | null;
    page_url: string | null;
    lang: string | null;
    is_lead: boolean;
    is_playground: boolean;
    started_at: string | null;
    messages: Message[];
};

type Props = { conversation: Conversation };

const ROLE_STYLES: Record<string, string> = {
    user: 'border-sky-200 bg-sky-50 text-sky-700 dark:border-sky-500/30 dark:bg-sky-500/10 dark:text-sky-300',
    assistant:
        'border-violet-200 bg-violet-50 text-violet-700 dark:border-violet-500/30 dark:bg-violet-500/10 dark:text-violet-300',
    system: 'border-border bg-muted text-muted-foreground',
};

export default function AdminConversationShow({ conversation }: Props) {
    const { t } = useT();

    return (
        <AdminLayout
            breadcrumbs={[
                { title: t('Admin'), href: '/admin' },
                { title: t('Conversations'), href: '/admin/conversations' },
                {
                    title: conversation.id.slice(0, 8),
                    href: `/admin/conversations/${conversation.id}`,
                },
            ]}
        >
            <Head title={t('Conversation · Admin')} />
            <AdminSurface>
                <AdminSurfaceBar>
                    <Link
                        href="/admin/conversations"
                        className="inline-flex items-center gap-1 text-sm text-muted-foreground hover:text-foreground"
                    >
                        <ArrowLeft className="size-4" />
                        {t('Back to conversations')}
                    </Link>
                </AdminSurfaceBar>

                <div className="grid gap-4 p-4 md:grid-cols-[1fr_280px]">
                    <Card className="p-0">
                        <div className="flex flex-col gap-3 border-b p-4">
                            <div className="flex items-center gap-2">
                                <MessageSquare className="size-4 text-muted-foreground" />
                                <h1 className="text-base font-semibold">
                                    {conversation.agent?.name ??
                                        t('Conversation')}
                                </h1>
                                {conversation.is_playground && (
                                    <Badge
                                        variant="outline"
                                        className="border-amber-200 bg-amber-50 text-amber-700 dark:border-amber-500/30 dark:bg-amber-500/10 dark:text-amber-300"
                                    >
                                        {t('Playground')}
                                    </Badge>
                                )}
                                {conversation.is_lead && (
                                    <Badge
                                        variant="outline"
                                        className="border-emerald-200 bg-emerald-50 text-emerald-700 dark:border-emerald-500/30 dark:bg-emerald-500/10 dark:text-emerald-300"
                                    >
                                        {t('Lead captured')}
                                    </Badge>
                                )}
                            </div>
                            {conversation.page_url && (
                                <p className="font-mono text-xs break-all text-muted-foreground">
                                    {conversation.page_url}
                                </p>
                            )}
                        </div>

                        <div className="flex flex-col gap-3 p-4">
                            {conversation.messages.length === 0 ? (
                                <p className="rounded-md border border-dashed bg-muted/30 p-6 text-center text-sm text-muted-foreground">
                                    {t(
                                        'No persisted messages for this conversation (e.g. playground runs intentionally skip the messages table).',
                                    )}
                                </p>
                            ) : (
                                conversation.messages.map((m) => (
                                    <div
                                        key={m.id}
                                        className={`rounded-md border p-3 text-sm whitespace-pre-wrap ${ROLE_STYLES[m.role] ?? ROLE_STYLES.system}`}
                                    >
                                        <div className="mb-1 text-[10px] font-semibold tracking-wider uppercase opacity-70">
                                            {m.role}
                                            {m.created_at && (
                                                <span className="ms-2 opacity-50">
                                                    {new Date(
                                                        m.created_at,
                                                    ).toLocaleString()}
                                                </span>
                                            )}
                                        </div>
                                        {m.content ?? ''}
                                    </div>
                                ))
                            )}
                        </div>
                    </Card>

                    <Card className="h-fit space-y-3 p-4 text-sm">
                        <div>
                            <p className="text-[11px] font-semibold tracking-wider text-muted-foreground uppercase">
                                {t('Workspace')}
                            </p>
                            <p className="mt-0.5 font-medium">
                                {conversation.workspace?.name ?? '—'}
                            </p>
                        </div>
                        <div>
                            <p className="text-[11px] font-semibold tracking-wider text-muted-foreground uppercase">
                                {t('Agent')}
                            </p>
                            <p className="mt-0.5 font-medium">
                                {conversation.agent?.name ?? '—'}
                            </p>
                        </div>
                        <div>
                            <p className="text-[11px] font-semibold tracking-wider text-muted-foreground uppercase">
                                {t('Started')}
                            </p>
                            <p className="mt-0.5 font-medium">
                                {conversation.started_at
                                    ? new Date(
                                          conversation.started_at,
                                      ).toLocaleString()
                                    : '—'}
                            </p>
                        </div>
                        <div>
                            <p className="text-[11px] font-semibold tracking-wider text-muted-foreground uppercase">
                                {t('Language')}
                            </p>
                            <p className="mt-0.5 font-medium uppercase">
                                {conversation.lang ?? '—'}
                            </p>
                        </div>
                    </Card>
                </div>
            </AdminSurface>
        </AdminLayout>
    );
}
