import { Head, Link } from '@inertiajs/react';
import type { ColumnDef } from '@tanstack/react-table';
import { useMemo } from 'react';
import { DataTable } from '@/components/data-table';
import { EmptyState } from '@/components/layout/empty-state';
import { PageHeader } from '@/components/layout/page-header';
import { SectionCard } from '@/components/layout/section-card';
import { StatusBadge } from '@/components/status-badge';
import AppLayout from '@/layouts/app-layout';

type PluginItem = {
    key: string;
    name: string;
    description: string;
    enabled: boolean;
    href?: string;
};

type Props = {
    plugins: PluginItem[];
};

export default function AdminPluginsIndex({ plugins }: Props) {
    const columns = useMemo<ColumnDef<PluginItem>[]>(
        () => [
            {
                accessorKey: 'name',
                header: 'Plugin',
                meta: { label: 'Plugin' },
                cell: ({ row }) =>
                    row.original.href ? (
                        <Link
                            href={row.original.href}
                            className="font-medium text-primary hover:underline"
                        >
                            {row.getValue('name')}
                        </Link>
                    ) : (
                        <span className="font-medium">{row.getValue('name')}</span>
                    ),
            },
            {
                accessorKey: 'description',
                header: 'Description',
                meta: { label: 'Description' },
                cell: ({ row }) => (
                    <span className="max-w-md whitespace-normal text-muted-foreground">
                        {row.getValue('description')}
                    </span>
                ),
            },
            {
                accessorKey: 'enabled',
                header: 'Status',
                meta: { label: 'Status' },
                cell: ({ row }) => (
                    <StatusBadge
                        status={row.getValue('enabled') ? 'enabled' : 'disabled'}
                    />
                ),
            },
        ],
        [],
    );

    return (
        <>
            <Head title="Plugin marketplace" />
            <div className="flex h-full flex-1 flex-col gap-6">
                <PageHeader title="Plugin marketplace" />

                <SectionCard title="Available plugins">
                    {plugins.length === 0 ? (
                        <EmptyState title="No plugins configured" />
                    ) : (
                        <DataTable
                            columns={columns}
                            data={plugins}
                            searchPlaceholder="Search plugins..."
                        />
                    )}
                </SectionCard>
            </div>
        </>
    );
}

AdminPluginsIndex.layout = (page: React.ReactNode) => (
    <AppLayout
        breadcrumbs={[
            {
                title: 'Plugin marketplace',
                href: '/admin/plugins',
            },
        ]}
    >
        {page}
    </AppLayout>
);
