import { Head, Link, usePage } from '@inertiajs/react';
import PublicLayout from '@/Layouts/PublicLayout';
import Card from '@/Components/UI/Card';
import Badge from '@/Components/UI/Badge';
import Button from '@/Components/UI/Button';
import { PageProps } from '@/types';

interface ThreadRow {
    id: number;
    title: string;
    slug: string;
    is_pinned: boolean;
    is_locked: boolean;
    author_name: string;
    posts_count: number;
    last_activity_at: string | null;
}

interface CategoryData {
    id: number;
    name: string;
    slug: string;
    description: string | null;
    admin_only_posting: boolean;
}

export default function Category({ category, threads }: { category: CategoryData; threads: ThreadRow[] }) {
    const { auth } = usePage<PageProps>().props;
    const canStartThread = auth?.user && (!category.admin_only_posting || auth.user.role === 'super_admin' || auth.user.role === 'academic_admin');

    return (
        <PublicLayout>
            <Head title={category.name} />

            <section className="mx-auto max-w-5xl px-6 pb-20 pt-16 lg:px-margin-page">
                <Link href={route('forum.index')} className="text-sm text-on-surface-variant hover:text-jgcp-navy">
                    &larr; All Categories
                </Link>

                <div className="mt-4 flex items-center justify-between">
                    <div>
                        <h1 className="text-institute-title text-on-surface">{category.name}</h1>
                        {category.description && (
                            <p className="text-body-md mt-2 max-w-2xl text-on-surface-variant">{category.description}</p>
                        )}
                    </div>
                    {canStartThread && (
                        <Button href={route('forum.threads.create', category.slug)} variant="action">
                            + New Thread
                        </Button>
                    )}
                </div>

                {!auth?.user && (
                    <p className="mt-4 text-sm text-on-surface-variant">
                        <Link href={route('login')} className="font-semibold text-jgcp-navy hover:underline">
                            Sign in
                        </Link>{' '}
                        to start a thread.
                    </p>
                )}

                <Card className="mt-8 overflow-x-auto p-0">
                    <table className="w-full text-left text-sm">
                        <thead className="border-b border-outline-variant text-on-surface-variant">
                            <tr>
                                <th className="px-6 py-3 font-medium">Thread</th>
                                <th className="px-6 py-3 font-medium">Started By</th>
                                <th className="px-6 py-3 font-medium">Replies</th>
                                <th className="px-6 py-3 font-medium">Last Activity</th>
                            </tr>
                        </thead>
                        <tbody>
                            {threads.map((thread) => (
                                <tr key={thread.id} className="border-b border-outline-variant last:border-0">
                                    <td className="px-6 py-3">
                                        <Link
                                            href={route('forum.thread', thread.slug)}
                                            className="font-medium text-on-surface hover:text-jgcp-navy hover:underline"
                                        >
                                            {thread.title}
                                        </Link>
                                        <div className="mt-1 flex gap-2">
                                            {thread.is_pinned && <Badge tone="gold">Pinned</Badge>}
                                            {thread.is_locked && <Badge tone="archived">Locked</Badge>}
                                        </div>
                                    </td>
                                    <td className="px-6 py-3 text-on-surface-variant">{thread.author_name}</td>
                                    <td className="px-6 py-3 text-on-surface-variant">{thread.posts_count - 1}</td>
                                    <td className="px-6 py-3 text-on-surface-variant">{thread.last_activity_at}</td>
                                </tr>
                            ))}
                            {threads.length === 0 && (
                                <tr>
                                    <td colSpan={4} className="px-6 py-6 text-center text-on-surface-variant">
                                        No threads yet in this category.
                                    </td>
                                </tr>
                            )}
                        </tbody>
                    </table>
                </Card>
            </section>
        </PublicLayout>
    );
}
