import React, { useState } from 'react';
import { useForm, usePage } from '@inertiajs/react';
import { AdminLayout } from '../AdminLayout';
import { SEOHead } from '@/Components/SEOHead';
import {
    User,
    Lock,
    Save,
    KeyRound,
    ShieldCheck,
    Mail,
    CheckCircle2,
    Calendar,
    BadgeCheck,
    ShieldAlert,
    Info,
    Eye,
    EyeOff,
    Sparkles,
    Sliders,
    Server,
} from 'lucide-react';

interface ProfileProps {
    user: any;
    stats?: {
        created_at: string;
        email_verified: boolean;
        role: string;
        is_admin: boolean;
    };
}

export default function Index({ user, stats }: ProfileProps) {
    const { flash } = usePage().props as any;

    const [showCurrentPassword, setShowCurrentPassword] = useState(false);
    const [showNewPassword, setShowNewPassword] = useState(false);
    const [showConfirmPassword, setShowConfirmPassword] = useState(false);

    const infoForm = useForm({
        name: user.name || '',
        email: user.email || '',
    });

    const passwordForm = useForm({
        current_password: '',
        password: '',
        password_confirmation: '',
    });

    const handleUpdateInfo = (e: React.FormEvent) => {
        e.preventDefault();
        infoForm.put('/admin/profile', {
            preserveScroll: true,
        });
    };

    const handleUpdatePassword = (e: React.FormEvent) => {
        e.preventDefault();
        passwordForm.put('/admin/profile/password', {
            preserveScroll: true,
            onSuccess: () => passwordForm.reset(),
        });
    };

    return (
        <AdminLayout title="My Account Profile & Security">
            <SEOHead title="My Profile | Tech in Soul Admin" description="Update your admin account profile details, authentication credentials, and security password." />

            <div className="w-full space-y-8">
                {/* Account Summary Header */}
                <div className="glass-card rounded-3xl p-6 sm:p-8 border border-slate-800 relative overflow-hidden">
                    <div className="absolute top-0 right-0 w-80 h-80 bg-gradient-to-bl from-[#00F2FE]/10 via-purple-600/5 to-transparent rounded-full blur-3xl -z-0 pointer-events-none" />

                    <div className="flex flex-col md:flex-row items-center md:items-start justify-between gap-6 relative z-10">
                        <div className="flex flex-col sm:flex-row items-center gap-5 text-center sm:text-left">
                            <div className="w-20 h-20 rounded-2xl bg-gradient-to-tr from-cyan-500/20 to-purple-500/20 border border-[#00F2FE]/40 flex items-center justify-center text-3xl font-extrabold text-[#00F2FE] shadow-lg shadow-cyan-500/10 shrink-0">
                                {user.name?.charAt(0) || 'A'}
                            </div>
                            <div className="space-y-1.5">
                                <div className="flex flex-wrap items-center justify-center sm:justify-start gap-2.5">
                                    <h2 className="text-2xl font-bold text-white tracking-tight">{user.name}</h2>
                                    <span className="text-[10px] font-bold px-3 py-1 rounded-full bg-[#00F2FE]/15 text-[#00F2FE] border border-[#00F2FE]/30 uppercase tracking-wider">
                                        {stats?.role || user.role || 'Super Admin'}
                                    </span>
                                    {stats?.email_verified !== false && (
                                        <span className="text-[10px] font-bold px-2.5 py-1 rounded-full bg-emerald-500/10 text-emerald-400 border border-emerald-500/20 flex items-center gap-1">
                                            <BadgeCheck className="w-3.5 h-3.5" /> Verified
                                        </span>
                                    )}
                                </div>
                                <p className="text-xs text-slate-400 font-mono flex items-center justify-center sm:justify-start gap-1.5">
                                    <Mail className="w-3.5 h-3.5 text-slate-500" /> {user.email}
                                </p>
                            </div>
                        </div>

                        {/* Account Stats Strip */}
                        <div className="grid grid-cols-2 sm:grid-cols-3 gap-3 w-full md:w-auto shrink-0">
                            <div className="p-3.5 rounded-2xl bg-[#060B18]/80 border border-slate-800 text-center space-y-0.5">
                                <span className="text-[10px] font-bold uppercase tracking-wider text-slate-500 block">Member Since</span>
                                <span className="text-xs font-bold text-white font-mono">{stats?.created_at || 'Recently'}</span>
                            </div>
                            <div className="p-3.5 rounded-2xl bg-[#060B18]/80 border border-slate-800 text-center space-y-0.5">
                                <span className="text-[10px] font-bold uppercase tracking-wider text-slate-500 block">Access Tier</span>
                                <span className="text-xs font-bold text-[#00F2FE]">Full Master</span>
                            </div>
                            <div className="p-3.5 rounded-2xl bg-[#060B18]/80 border border-slate-800 text-center space-y-0.5 col-span-2 sm:col-span-1">
                                <span className="text-[10px] font-bold uppercase tracking-wider text-slate-500 block">System Status</span>
                                <span className="text-xs font-bold text-emerald-400 flex items-center justify-center gap-1">
                                    <span className="w-1.5 h-1.5 rounded-full bg-emerald-400 animate-pulse" /> Active
                                </span>
                            </div>
                        </div>
                    </div>
                </div>

                {/* Main Forms Grid */}
                <div className="grid grid-cols-1 lg:grid-cols-2 gap-8">
                    {/* Update Info Form */}
                    <div className="glass-card rounded-3xl p-6 sm:p-8 space-y-6 border border-slate-800 flex flex-col justify-between">
                        <form onSubmit={handleUpdateInfo} className="space-y-6">
                            <div className="flex items-center gap-2.5 text-white font-bold text-base border-b border-slate-800 pb-4">
                                <div className="p-2 rounded-xl bg-cyan-500/10 text-[#00F2FE]">
                                    <User className="w-4 h-4" />
                                </div>
                                <div>
                                    <h3 className="text-sm font-bold text-white">General Information</h3>
                                    <p className="text-[11px] font-normal text-slate-400">Update your account name and email address used for admin login.</p>
                                </div>
                            </div>

                            {/* Help / Guidance Box */}
                            <div className="p-4 rounded-2xl bg-[#060B18] border border-cyan-500/20 text-xs text-slate-400 space-y-1.5">
                                <div className="flex items-center gap-1.5 text-cyan-300 font-bold text-[11px] uppercase tracking-wider">
                                    <Info className="w-3.5 h-3.5" /> Notice on Email Changes
                                </div>
                                <p className="text-[11px] leading-relaxed text-slate-300">
                                    Changing your email address will immediately change your login username and all system notification dispatch targets.
                                </p>
                            </div>

                            <div className="space-y-4">
                                <div>
                                    <label className="text-xs font-bold text-slate-300 uppercase block mb-1.5">Full Name *</label>
                                    <input
                                        type="text"
                                        value={infoForm.data.name}
                                        onChange={(e) => infoForm.setData('name', e.target.value)}
                                        placeholder="Your Full Name"
                                        required
                                        className="w-full bg-[#060B18] border border-slate-800 rounded-xl px-4 py-2.5 text-sm text-white focus:outline-none focus:border-[#00F2FE] transition-colors"
                                    />
                                    {infoForm.errors.name && <p className="text-xs text-rose-400 mt-1">{infoForm.errors.name}</p>}
                                </div>

                                <div>
                                    <label className="text-xs font-bold text-slate-300 uppercase block mb-1.5">Email Address *</label>
                                    <input
                                        type="email"
                                        value={infoForm.data.email}
                                        onChange={(e) => infoForm.setData('email', e.target.value)}
                                        placeholder="admin@techinsoul.com"
                                        required
                                        className="w-full bg-[#060B18] border border-slate-800 rounded-xl px-4 py-2.5 text-sm text-white focus:outline-none focus:border-[#00F2FE] font-mono transition-colors"
                                    />
                                    {infoForm.errors.email && <p className="text-xs text-rose-400 mt-1">{infoForm.errors.email}</p>}
                                </div>
                            </div>

                            <div className="flex justify-end pt-2">
                                <button
                                    type="submit"
                                    disabled={infoForm.processing}
                                    className="btn-aqua w-full sm:w-auto px-6 py-2.5 rounded-xl font-bold text-xs flex items-center justify-center gap-2 disabled:opacity-50"
                                >
                                    <Save className="w-4 h-4" /> Save Profile Info
                                </button>
                            </div>
                        </form>
                    </div>

                    {/* Change Password Form */}
                    <div className="glass-card rounded-3xl p-6 sm:p-8 space-y-6 border border-slate-800 flex flex-col justify-between">
                        <form onSubmit={handleUpdatePassword} className="space-y-6">
                            <div className="flex items-center gap-2.5 text-white font-bold text-base border-b border-slate-800 pb-4">
                                <div className="p-2 rounded-xl bg-purple-500/10 text-purple-400">
                                    <KeyRound className="w-4 h-4" />
                                </div>
                                <div>
                                    <h3 className="text-sm font-bold text-white">Security & Password</h3>
                                    <p className="text-[11px] font-normal text-slate-400">Ensure your account is using a secure, strong password.</p>
                                </div>
                            </div>

                            {/* Security Checklist Box */}
                            <div className="p-4 rounded-2xl bg-[#060B18] border border-slate-800 text-xs space-y-2">
                                <div className="flex items-center gap-1.5 text-slate-300 font-bold text-[11px] uppercase tracking-wider">
                                    <ShieldCheck className="w-3.5 h-3.5 text-emerald-400" /> Password Security Guidelines
                                </div>
                                <ul className="text-[11px] text-slate-400 space-y-1 list-disc list-inside">
                                    <li>Minimum of 8 characters (12+ recommended)</li>
                                    <li>Includes uppercase and lowercase letters</li>
                                    <li>Contains at least one number and special symbol</li>
                                </ul>
                            </div>

                            <div className="space-y-4">
                                <div>
                                    <label className="text-xs font-bold text-slate-300 uppercase block mb-1.5">Current Password *</label>
                                    <div className="relative">
                                        <input
                                            type={showCurrentPassword ? 'text' : 'password'}
                                            value={passwordForm.data.current_password}
                                            onChange={(e) => passwordForm.setData('current_password', e.target.value)}
                                            placeholder="Enter your current password"
                                            required
                                            className="w-full bg-[#060B18] border border-slate-800 rounded-xl pl-4 pr-10 py-2.5 text-sm text-white focus:outline-none focus:border-[#00F2FE] transition-colors"
                                        />
                                        <button
                                            type="button"
                                            onClick={() => setShowCurrentPassword(!showCurrentPassword)}
                                            className="absolute right-3 top-1/2 -translate-y-1/2 text-slate-400 hover:text-white p-1 rounded-md"
                                        >
                                            {showCurrentPassword ? <EyeOff className="w-4 h-4" /> : <Eye className="w-4 h-4" />}
                                        </button>
                                    </div>
                                    {passwordForm.errors.current_password && (
                                        <p className="text-xs text-rose-400 mt-1">{passwordForm.errors.current_password}</p>
                                    )}
                                </div>

                                <div>
                                    <label className="text-xs font-bold text-slate-300 uppercase block mb-1.5">New Password *</label>
                                    <div className="relative">
                                        <input
                                            type={showNewPassword ? 'text' : 'password'}
                                            value={passwordForm.data.password}
                                            onChange={(e) => passwordForm.setData('password', e.target.value)}
                                            placeholder="Enter strong new password"
                                            required
                                            className="w-full bg-[#060B18] border border-slate-800 rounded-xl pl-4 pr-10 py-2.5 text-sm text-white focus:outline-none focus:border-[#00F2FE] transition-colors"
                                        />
                                        <button
                                            type="button"
                                            onClick={() => setShowNewPassword(!showNewPassword)}
                                            className="absolute right-3 top-1/2 -translate-y-1/2 text-slate-400 hover:text-white p-1 rounded-md"
                                        >
                                            {showNewPassword ? <EyeOff className="w-4 h-4" /> : <Eye className="w-4 h-4" />}
                                        </button>
                                    </div>
                                    {passwordForm.errors.password && (
                                        <p className="text-xs text-rose-400 mt-1">{passwordForm.errors.password}</p>
                                    )}
                                </div>

                                <div>
                                    <label className="text-xs font-bold text-slate-300 uppercase block mb-1.5">Confirm New Password *</label>
                                    <div className="relative">
                                        <input
                                            type={showConfirmPassword ? 'text' : 'password'}
                                            value={passwordForm.data.password_confirmation}
                                            onChange={(e) => passwordForm.setData('password_confirmation', e.target.value)}
                                            placeholder="Repeat new password"
                                            required
                                            className="w-full bg-[#060B18] border border-slate-800 rounded-xl pl-4 pr-10 py-2.5 text-sm text-white focus:outline-none focus:border-[#00F2FE] transition-colors"
                                        />
                                        <button
                                            type="button"
                                            onClick={() => setShowConfirmPassword(!showConfirmPassword)}
                                            className="absolute right-3 top-1/2 -translate-y-1/2 text-slate-400 hover:text-white p-1 rounded-md"
                                        >
                                            {showConfirmPassword ? <EyeOff className="w-4 h-4" /> : <Eye className="w-4 h-4" />}
                                        </button>
                                    </div>
                                </div>
                            </div>

                            <div className="flex justify-end pt-2">
                                <button
                                    type="submit"
                                    disabled={passwordForm.processing}
                                    className="btn-aqua w-full sm:w-auto px-6 py-2.5 rounded-xl font-bold text-xs flex items-center justify-center gap-2 disabled:opacity-50"
                                >
                                    <Lock className="w-4 h-4" /> Update Password
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </AdminLayout>
    );
}

