import AdminLayout from '@/Layouts/AdminLayout';
import { Head, useForm, Link } from '@inertiajs/react';
import { Save, ArrowLeft, CreditCard, Lock, HelpCircle } from 'lucide-react';
import { toast } from 'sonner';

interface Props {
    settings: {
        paystack_public_key: string;
        paystack_secret_key: string;
        paystack_currency: string;
        paystack_enabled: boolean;
    };
}

export default function Payment({ settings }: Props) {
    const { data, setData, patch, processing, errors } = useForm({
        paystack_public_key: settings.paystack_public_key || '',
        paystack_secret_key: settings.paystack_secret_key || '',
        paystack_currency: settings.paystack_currency || 'NGN',
        paystack_enabled: settings.paystack_enabled || false,
    });

    const submit = (e: React.FormEvent) => {
        e.preventDefault();
        patch(route('admin.settings.payment.update'), {
            onSuccess: () => toast.success('Payment settings updated'),
        });
    };

    return (
        <AdminLayout>
            <Head title="Payment Settings" />

            <div className="mb-8">
                <Link 
                    href={route('admin.settings.index')}
                    className="inline-flex items-center gap-2 text-[11px] font-bold uppercase tracking-widest text-[#8C92AC] hover:text-ink transition-colors mb-4"
                >
                    <ArrowLeft className="h-3 w-3" />
                    Back to Settings
                </Link>
                <h1 className="font-display text-[1.625rem] font-normal tracking-[-0.02em] text-ink leading-none">
                    Store & Payments
                </h1>
                <p className="mt-2.5 text-[0.9375rem] text-[#8C92AC]">
                    Configure your payment gateway credentials and preferences.
                </p>
            </div>

            <div className="max-w-3xl">
                <form onSubmit={submit} className="space-y-6">
                    <div className="bg-white rounded-2xl border border-[#E8E6DF] overflow-hidden">
                        <div className="p-6 border-b border-[#E8E6DF] flex items-center justify-between">
                            <div className="flex items-center gap-3">
                                <div className="flex h-10 w-10 items-center justify-center rounded-xl bg-orange-50 text-orange-600">
                                    <CreditCard className="h-5 w-5" />
                                </div>
                                <div>
                                    <h3 className="text-[15px] font-bold text-ink">Paystack Integration</h3>
                                    <p className="text-[12px] text-[#8C92AC] mt-0.5">Collect payments on checkout using Paystack.</p>
                                </div>
                            </div>
                            <div>
                                <label className="relative inline-flex items-center cursor-pointer">
                                    <input 
                                        type="checkbox" 
                                        checked={data.paystack_enabled}
                                        onChange={e => setData('paystack_enabled', e.target.checked)}
                                        className="sr-only peer"
                                    />
                                    <div className="w-11 h-6 bg-[#F0EDE8] peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-accent"></div>
                                </label>
                            </div>
                        </div>

                        <div className={`p-6 space-y-6 transition-opacity ${data.paystack_enabled ? 'opacity-100' : 'opacity-60'}`}>
                            <div className="grid gap-6 sm:grid-cols-2">
                                <div>
                                    <label className="text-[12px] font-bold text-[#8C92AC] uppercase tracking-wider block mb-2 flex items-center gap-1.5">
                                        <Lock className="h-3.5 w-3.5 text-[#C0BDBA]" />
                                        Paystack Public Key
                                    </label>
                                    <input 
                                        type="text"
                                        disabled={!data.paystack_enabled}
                                        value={data.paystack_public_key}
                                        onChange={e => setData('paystack_public_key', e.target.value)}
                                        className="w-full px-4 py-2.5 bg-white border border-[#E8E6DF] rounded-xl text-[14px] focus:border-accent outline-none transition-all disabled:bg-secondary/20 font-mono"
                                        placeholder="pk_test_..."
                                    />
                                    {errors.paystack_public_key && <p className="text-red-500 text-xs mt-1">{errors.paystack_public_key}</p>}
                                </div>

                                <div>
                                    <label className="text-[12px] font-bold text-[#8C92AC] uppercase tracking-wider block mb-2 flex items-center gap-1.5">
                                        <Lock className="h-3.5 w-3.5 text-[#C0BDBA]" />
                                        Paystack Secret Key
                                    </label>
                                    <input 
                                        type="password"
                                        disabled={!data.paystack_enabled}
                                        value={data.paystack_secret_key}
                                        onChange={e => setData('paystack_secret_key', e.target.value)}
                                        className="w-full px-4 py-2.5 bg-white border border-[#E8E6DF] rounded-xl text-[14px] focus:border-accent outline-none transition-all disabled:bg-secondary/20 font-mono"
                                        placeholder="sk_test_..."
                                    />
                                    {errors.paystack_secret_key && <p className="text-red-500 text-xs mt-1">{errors.paystack_secret_key}</p>}
                                </div>
                            </div>

                            <div>
                                <label className="text-[12px] font-bold text-[#8C92AC] uppercase tracking-wider block mb-2">
                                    Default Currency
                                </label>
                                <select
                                    disabled={!data.paystack_enabled}
                                    value={data.paystack_currency}
                                    onChange={e => setData('paystack_currency', e.target.value)}
                                    className="w-full px-4 py-2.5 bg-white border border-[#E8E6DF] rounded-xl text-[14px] focus:border-accent outline-none transition-all disabled:bg-secondary/20"
                                >
                                    <option value="NGN">NGN (Nigerian Naira)</option>
                                    <option value="USD">USD (United States Dollar)</option>
                                    <option value="GHS">GHS (Ghanaian Cedi)</option>
                                    <option value="ZAR">ZAR (South African Rand)</option>
                                </select>
                                {errors.paystack_currency && <p className="text-red-500 text-xs mt-1">{errors.paystack_currency}</p>}
                            </div>

                            <div className="p-4 bg-orange-50/50 border border-orange-100 rounded-xl text-[13px] text-orange-800 leading-relaxed flex gap-3">
                                <HelpCircle className="h-5 w-5 shrink-0 text-orange-500 mt-0.5" />
                                <div>
                                    <p className="font-bold">Webhook Configuration</p>
                                    <p className="mt-1">
                                        For seamless transactions, please configure the Webhook URL in your Paystack Dashboard to point to:
                                    </p>
                                    <code className="mt-2 block bg-white/80 p-2 rounded border border-orange-100/50 font-mono select-all text-xs">
                                        {typeof window !== 'undefined' ? `${window.location.origin}/store/payment/webhook` : 'https://yourdomain.com/store/payment/webhook'}
                                    </code>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div className="flex justify-end">
                        <button 
                            type="submit" 
                            disabled={processing}
                            className="w-full sm:w-auto px-10 py-3.5 bg-ink text-white rounded-2xl text-[15px] font-semibold flex items-center justify-center gap-2 hover:bg-accent transition-all shadow-lg shadow-ink/10 active:scale-[0.98] disabled:opacity-50"
                        >
                            <Save className="h-4 w-4" />
                            {processing ? 'Saving...' : 'Save Settings'}
                        </button>
                    </div>
                </form>
            </div>
        </AdminLayout>
    );
}
