import React, { useState } from 'react';
import { Head, useForm } from '@inertiajs/react';
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
import { useInitials } from '@/hooks/use-initials';
import type { User } from '@/types';

type Props = {
    auth: {
        user: User;
    };
    status?: string;
};

export default function Edit({ auth, status }: Props) {
    const user = auth.user;
    const getInitials = useInitials();
    const [successMessage, setSuccessMessage] = useState(status || '');

    const { data, setData, put, processing, errors } = useForm({
        name: user.name,
        email: user.email,
    });

    const handleSubmit = (e: React.FormEvent) => {
        e.preventDefault();
        setSuccessMessage('');
    };

    return (
        <>
            <Head title="Profile Settings" />

            <div className="min-h-screen space-y-8 bg-[#070c14] p-6 text-slate-100 md:p-10">
                {/* HEADER */}
                <div>
                    <h1 className="text-3xl font-bold tracking-tight text-white">
                        Account Settings
                    </h1>
                    <p className="mt-1 text-sm text-slate-400">
                        Kelola informasi profil dan pengaturan akun Scuto Legal
                        Anda.
                    </p>
                </div>

                <hr className="border-slate-800/60" />

                {/* NOTIFIKASI */}
                {successMessage && (
                    <div className="rounded-lg border border-emerald-500/20 bg-emerald-950/40 p-4 text-sm text-emerald-400">
                        {successMessage}
                    </div>
                )}

                {/* GRID KONTEN */}
                <div className="grid grid-cols-1 items-start gap-8 lg:grid-cols-3">
                    {/* Card Avatar Detail */}
                    <div className="flex flex-col items-center justify-center space-y-4 rounded-xl border border-slate-800/60 bg-[#0b121f] p-6 text-center shadow-xl">
                        <Avatar className="h-24 w-24 overflow-hidden rounded-full border-2 border-orange-500/30 p-1">
                            <AvatarImage src={user.avatar} alt={user.name} />
                            <AvatarFallback className="rounded-full bg-orange-600 text-2xl font-bold text-white">
                                {getInitials(user.name)}
                            </AvatarFallback>
                        </Avatar>

                        <div>
                            <h2 className="text-xl font-semibold tracking-tight text-white">
                                {user.name}
                            </h2>
                            <p className="mt-0.5 text-xs font-medium text-slate-400">
                                Scuto Legal Member
                            </p>
                        </div>

                        <span className="inline-flex items-center rounded-full border border-orange-500/20 bg-slate-900 px-3 py-1 text-xs font-medium text-orange-400">
                            Active Session
                        </span>
                    </div>

                    {/* Form Edit */}
                    <form
                        onSubmit={handleSubmit}
                        className="space-y-6 rounded-xl border border-slate-800/60 bg-[#0b121f] p-6 shadow-xl lg:col-span-2"
                    >
                        <h3 className="border-b border-slate-800/80 pb-3 text-lg font-medium text-white">
                            Personal Information
                        </h3>

                        <div className="grid grid-cols-1 gap-6 md:grid-cols-2">
                            {/* Input Nama */}
                            <div className="space-y-2">
                                <label className="text-xs font-semibold tracking-wider text-slate-400 uppercase">
                                    Full Name
                                </label>
                                <input
                                    type="text"
                                    value={data.name}
                                    onChange={(e) =>
                                        setData('name', e.target.value)
                                    }
                                    className={`w-full rounded-lg border bg-[#111a2e] px-3 py-2.5 text-sm text-white transition-colors focus:border-orange-500 focus:ring-1 focus:ring-orange-500 focus:outline-none ${
                                        errors.name
                                            ? 'border-rose-500'
                                            : 'border-slate-800'
                                    }`}
                                    placeholder="Masukkan nama lengkap"
                                    required
                                />
                                {errors.name && (
                                    <p className="mt-1 text-xs text-rose-500">
                                        {errors.name}
                                    </p>
                                )}
                            </div>

                            {/* Input Email */}
                            <div className="space-y-2">
                                <label className="text-xs font-semibold tracking-wider text-slate-400 uppercase">
                                    Email Address
                                </label>
                                <input
                                    type="email"
                                    value={data.email}
                                    onChange={(e) =>
                                        setData('email', e.target.value)
                                    }
                                    className={`w-full rounded-lg border bg-[#111a2e] px-3 py-2.5 text-sm text-white transition-colors focus:border-orange-500 focus:ring-1 focus:ring-orange-500 focus:outline-none ${
                                        errors.email
                                            ? 'border-rose-500'
                                            : 'border-slate-800'
                                    }`}
                                    placeholder="nama@email.com"
                                    required
                                />
                                {errors.email && (
                                    <p className="mt-1 text-xs text-rose-500">
                                        {errors.email}
                                    </p>
                                )}
                            </div>
                        </div>

                        {/* save  */}
                        <div className="flex justify-end border-t border-slate-800/80 pt-4">
                            <button
                                type="submit"
                                disabled={processing}
                                className="rounded-lg bg-orange-600 px-5 py-2.5 text-sm font-medium text-white shadow-lg shadow-orange-600/10 transition-colors hover:bg-orange-700 focus:outline-none disabled:bg-orange-800"
                            >
                                {processing ? 'Saving...' : 'Save Changes'}
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </>
    );
}
