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

import HeadingSmall from '@/components/heading-small';
import AppLayout from '@/layouts/app-layout';
import { index, toggle } from '@/actions/App/Http/Controllers/Admin/IntegrationController';
import { index as settingsIndex } from '@/routes/admin/integrations/settings';

type Integration = {
    id: number;
    key: string;
    name: string;
    description: string | null;
    enabled: boolean;
    integration_logs_count: number;
};

type Props = {
    integrations: Integration[];
};

const breadcrumbs = [{ title: 'Integrations', href: index.url() }];

export default function Integrations({ integrations }: Props) {
    return (
        <AppLayout breadcrumbs={breadcrumbs}>
            <Head title="Integrations" />

            <h1 className="sr-only">Integrations</h1>

            <div className="space-y-6">
                <HeadingSmall
                    title="Integrations"
                    description="Enable or disable third-party integrations."
                />

                <div className="overflow-hidden rounded-md border border-zinc-200 bg-white dark:border-zinc-800 dark:bg-zinc-950">
                    <div className="overflow-x-auto">
                        <table className="w-full text-left text-sm">
                            <thead className="border-b border-zinc-200 text-xs uppercase text-zinc-600 dark:border-zinc-800 dark:text-zinc-400">
                                <tr>
                                    <th className="px-4 py-3">Name</th>
                                    <th className="px-4 py-3">Key</th>
                                    <th className="px-4 py-3">Description</th>
                                    <th className="px-4 py-3">Log Entries</th>
                                    <th className="px-4 py-3">Status</th>
                                    <th className="px-4 py-3" />
                                </tr>
                            </thead>
                            <tbody className="divide-y divide-zinc-200 dark:divide-zinc-800">
                                {integrations.map((integration) => (
                                    <tr key={integration.id}>
                                        <td className="px-4 py-3 font-medium">{integration.name}</td>
                                        <td className="px-4 py-3 font-mono text-xs text-zinc-600 dark:text-zinc-400">
                                            {integration.key}
                                        </td>
                                        <td className="px-4 py-3 text-zinc-600 dark:text-zinc-400">
                                            {integration.description ?? '—'}
                                        </td>
                                        <td className="px-4 py-3">{integration.integration_logs_count}</td>
                                        <td className="px-4 py-3">
                                            <span
                                                className={`inline-block rounded-full px-2 py-0.5 text-xs font-medium ${
                                                    integration.enabled
                                                        ? 'bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-300'
                                                        : 'bg-zinc-100 text-zinc-600 dark:bg-zinc-800 dark:text-zinc-400'
                                                }`}
                                            >
                                                {integration.enabled ? 'Enabled' : 'Disabled'}
                                            </span>
                                        </td>
                                        <td className="px-4 py-3 text-right">
                                            <div className="flex items-center justify-end gap-3">
                                                <Link
                                                    href={settingsIndex.url(integration.id)}
                                                    className="text-xs font-medium text-zinc-700 hover:underline dark:text-zinc-200"
                                                >
                                                    Settings
                                                </Link>
                                                <button
                                                    type="button"
                                                    onClick={() => router.patch(toggle.url(integration.id), {}, { preserveScroll: true })}
                                                    className="rounded-md border border-zinc-200 bg-white px-2.5 py-1 text-xs font-medium text-zinc-700 hover:bg-zinc-50 dark:border-zinc-700 dark:bg-zinc-900 dark:text-zinc-200 dark:hover:bg-zinc-800"
                                                >
                                                    {integration.enabled ? 'Disable' : 'Enable'}
                                                </button>
                                            </div>
                                        </td>
                                    </tr>
                                ))}
                            </tbody>
                        </table>
                    </div>

                    {integrations.length === 0 && (
                        <div className="p-4 text-sm text-zinc-600 dark:text-zinc-400">
                            No integrations configured yet.
                        </div>
                    )}
                </div>
            </div>
        </AppLayout>
    );
}
