import { router } from '@inertiajs/react';
import { Check, Copy, Bot, ChevronDown, Trash2, AlertTriangle } from 'lucide-react';
import { useState } from 'react';
import { useTranslation } from 'react-i18next';

/* Small building blocks shared by the SMS and website-chat setup panels. */

export const inputClass = 'w-full rounded-lg border border-neutral-200 dark:border-neutral-600 bg-white dark:bg-neutral-800 px-3 py-2 text-sm text-neutral-900 dark:text-neutral-100 focus:outline-none focus:ring-2 focus:ring-brand-500/30 focus:border-brand-400 transition';

export function Field({ label, help, error, children }) {
    return (
        <label className="block space-y-1">
            <span className="text-xs font-medium text-neutral-600 dark:text-neutral-300">{label}</span>
            {children}
            {help && <span className="block text-[11px] leading-relaxed text-neutral-400 dark:text-neutral-500">{help}</span>}
            {error && <span className="block text-xs text-red-500">{error}</span>}
        </label>
    );
}

export function CopyRow({ label, value }) {
    const { t } = useTranslation();
    const [copied, setCopied] = useState(false);
    const copy = () => navigator.clipboard?.writeText(value).then(() => {
        setCopied(true);
        setTimeout(() => setCopied(false), 2000);
    });

    return (
        <div className="space-y-1">
            {label && <p className="text-[11px] font-medium uppercase tracking-wider text-neutral-400">{label}</p>}
            <div className="flex items-start gap-1.5 rounded-lg border border-neutral-200 dark:border-neutral-700 bg-white dark:bg-neutral-900 px-2.5 py-1.5">
                <code className="flex-1 min-w-0 break-all font-mono text-[11px] text-neutral-700 dark:text-neutral-300 whitespace-pre-wrap">{value}</code>
                <button type="button" onClick={copy} title={t('inbox.copy')}
                    className="shrink-0 rounded-md p-1 text-neutral-400 hover:text-brand-600 transition">
                    {copied ? <Check className="h-3.5 w-3.5 text-green-500" /> : <Copy className="h-3.5 w-3.5" />}
                </button>
            </div>
        </div>
    );
}

export function ChatbotSelect({ accountId, current, chatbots }) {
    const { t } = useTranslation();
    const [value, setValue] = useState(current ? String(current) : '');
    if (!chatbots?.length) return null;

    const change = (e) => {
        setValue(e.target.value);
        router.patch(route('client.inbox.setup.assign-chatbot', { channelAccount: accountId }),
            { chatbot_id: e.target.value === '' ? null : Number(e.target.value) },
            { preserveScroll: true });
    };

    return (
        <div className="flex items-center gap-2">
            <Bot className="h-3.5 w-3.5 text-brand-500 shrink-0" />
            <div className="relative flex-1">
                <select value={value} onChange={change}
                    className="w-full appearance-none rounded-lg border border-neutral-200 dark:border-neutral-600 bg-white dark:bg-neutral-800 pl-2.5 pr-7 py-1.5 text-xs focus:outline-none focus:ring-2 focus:ring-brand-500/30">
                    <option value="">{t('inbox.no_chatbot')}</option>
                    {chatbots.map(bot => <option key={bot.id} value={String(bot.id)}>{bot.name}</option>)}
                </select>
                <ChevronDown className="pointer-events-none absolute right-2 top-1/2 -translate-y-1/2 h-3 w-3 text-neutral-400" />
            </div>
        </div>
    );
}

export function RemoveButton({ accountId }) {
    const { t } = useTranslation();
    const [confirm, setConfirm] = useState(false);

    if (!confirm) {
        return (
            <button type="button" onClick={() => setConfirm(true)} title={t('channels.disconnect')}
                className="rounded-lg border border-red-200 dark:border-red-800 p-1.5 text-red-400 hover:bg-red-50 dark:hover:bg-red-950/40 hover:text-red-600 transition">
                <Trash2 className="h-3.5 w-3.5" />
            </button>
        );
    }

    return (
        <span className="flex items-center gap-1.5">
            <AlertTriangle className="h-3.5 w-3.5 text-red-500" />
            <button type="button"
                onClick={() => router.delete(route('client.inbox.setup.destroy', { channelAccount: accountId }), { preserveScroll: true })}
                className="rounded-lg bg-red-600 px-2.5 py-1 text-xs font-medium text-white hover:bg-red-700 transition">
                {t('inbox.confirm')}
            </button>
            <button type="button" onClick={() => setConfirm(false)}
                className="rounded-lg border border-neutral-200 dark:border-neutral-600 px-2.5 py-1 text-xs text-neutral-600 dark:text-neutral-400">
                {t('common.cancel')}
            </button>
        </span>
    );
}

export function PanelShell({ icon: Icon, iconClass, title, subtitle, count, action, children }) {
    const { t } = useTranslation();
    return (
        <div className="rounded-2xl border border-neutral-200 dark:border-neutral-700 bg-white dark:bg-neutral-900 shadow-sm overflow-hidden">
            <div className="flex items-center gap-3 px-5 py-4 border-b border-neutral-100 dark:border-neutral-800">
                <div className={`rounded-xl p-2 ${iconClass}`}><Icon className="h-4 w-4" /></div>
                <div className="min-w-0">
                    <h3 className="font-semibold text-sm text-neutral-900 dark:text-neutral-100">{title}</h3>
                    {subtitle && <p className="text-xs text-neutral-500 dark:text-neutral-400">{subtitle}</p>}
                </div>
                <span className={`ml-auto text-xs font-medium px-2 py-0.5 rounded-full whitespace-nowrap ${count > 0 ? 'bg-emerald-50 text-emerald-700 dark:bg-emerald-900/30 dark:text-emerald-300' : 'bg-neutral-100 text-neutral-500 dark:bg-neutral-800 dark:text-neutral-400'}`}>
                    {t('inbox.count_connected', { count })}
                </span>
                {action}
            </div>
            <div className="p-5 space-y-3">{children}</div>
        </div>
    );
}
