import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout';
import { Head, Link, useForm } from '@inertiajs/react';
import { Users, ArrowLeft, Check, Lock, Mail, User, AlertCircle } from 'lucide-react';
import { Button } from '@/Components/ui/button';
import { Input } from '@/Components/ui/input';
import { Label } from '@/Components/ui/label';
import { Checkbox } from '@/Components/ui/checkbox';
import { FormEvent } from 'react';

interface Role {
    id: number;
    name: string;
}

interface UserData {
    id: number;
    name: string;
    email: string;
    username: string | null;
}

interface Props {
    user: UserData;
    roles: Role[];
    currentRoles: string[];
}

export default function UserEdit({ user, roles, currentRoles }: Props) {
    const { data, setData, put, processing, errors } = useForm({
        name: user.name,
        email: user.email,
        username: user.username || '',
        password: '',
        password_confirmation: '',
        roles: currentRoles,
    });

    const handleSubmit = (e: FormEvent) => {
        e.preventDefault();
        put(route('users.update', user.id));
    };

    const toggleRole = (roleName: string) => {
        if (data.roles.includes(roleName)) {
            setData('roles', data.roles.filter(r => r !== roleName));
        } else {
            setData('roles', [...data.roles, roleName]);
        }
    };

    const translateRoleName = (name: string) => {
        const map: Record<string, string> = {
            'super-admin': '超級管理員',
            'admin': '管理員',
            'warehouse-manager': '倉庫主管',
            'purchaser': '採購人員',
            'viewer': '檢視者',
        };
        return map[name] || name;
    }

    return (
        <AuthenticatedLayout
            breadcrumbs={[
                { label: '系統管理', href: '#' },
                { label: '使用者管理', href: route('users.index') },
                { label: '編輯使用者', href: route('users.edit', user.id), isPage: true },
            ]}
        >
            <Head title={`編輯使用者 - ${user.name}`} />

            <div className="p-8 max-w-4xl mx-auto">
                <form onSubmit={handleSubmit} className="space-y-8">
                    {/* Header */}
                    <div className="flex items-center justify-between">
                        <div>
                            <h1 className="text-2xl font-bold text-grey-0 flex items-center gap-2">
                                <Users className="h-6 w-6 text-[#01ab83]" />
                                編輯使用者
                            </h1>
                            <p className="text-gray-500 mt-1">
                                修改使用者資料、重設密碼或變更角色
                            </p>
                        </div>
                        <div className="flex items-center gap-3">
                            <Link href={route('users.index')}>
                                <Button variant="outline" type="button">
                                    <ArrowLeft className="h-4 w-4 mr-2" />
                                    取消
                                </Button>
                            </Link>
                            <Button
                                type="submit"
                                className="bg-[#01ab83] hover:bg-[#019a76]"
                                disabled={processing}
                            >
                                <Check className="h-4 w-4 mr-2" />
                                儲存變更
                            </Button>
                        </div>
                    </div>

                    <div className="grid grid-cols-1 md:grid-cols-3 gap-6">
                        {/* Basic Info */}
                        <div className="md:col-span-2 space-y-6">
                            <div className="bg-white p-6 rounded-xl border border-gray-200 shadow-sm space-y-6">
                                <h3 className="font-bold text-gray-900 border-b pb-2 mb-4">基本資料</h3>

                                <div className="space-y-2">
                                    <Label htmlFor="name" className="flex items-center gap-2">
                                        <User className="h-4 w-4" /> 姓名
                                    </Label>
                                    <Input
                                        id="name"
                                        value={data.name}
                                        onChange={e => setData('name', e.target.value)}
                                        placeholder="例如：王小明"
                                    />
                                    {errors.name && <p className="text-sm text-red-500">{errors.name}</p>}
                                </div>

                                <div className="space-y-2">
                                    <Label htmlFor="email" className="flex items-center gap-2">
                                        <Mail className="h-4 w-4" /> 電子郵件 (選填)
                                    </Label>
                                    <Input
                                        id="email"
                                        type="email"
                                        value={data.email}
                                        onChange={e => setData('email', e.target.value)}
                                        placeholder="user@example.com (可省略)"
                                    />
                                    {errors.email && <p className="text-sm text-red-500">{errors.email}</p>}
                                </div>

                                <div className="space-y-2">
                                    <Label htmlFor="username" className="flex items-center gap-2">
                                        <User className="h-4 w-4" /> 使用者名稱 (登入用)
                                    </Label>
                                    <Input
                                        id="username"
                                        value={data.username}
                                        onChange={e => setData('username', e.target.value)}
                                        placeholder="請輸入登入帳號"
                                    />
                                    {errors.username && <p className="text-sm text-red-500">{errors.username}</p>}
                                </div>
                            </div>

                            <div className="bg-white p-6 rounded-xl border border-gray-200 shadow-sm space-y-6">
                                <h3 className="font-bold text-gray-900 border-b pb-2 mb-4">重設密碼</h3>
                                <div className="bg-amber-50 text-amber-800 p-3 rounded-lg text-sm flex items-start gap-2 mb-4">
                                    <AlertCircle className="h-4 w-4 mt-0.5 shrink-0" />
                                    若不修改密碼，請留空以下欄位。
                                </div>

                                <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
                                    <div className="space-y-2">
                                        <Label htmlFor="password" className="flex items-center gap-2">
                                            <Lock className="h-4 w-4" /> 新密碼
                                        </Label>
                                        <Input
                                            id="password"
                                            type="password"
                                            value={data.password}
                                            onChange={e => setData('password', e.target.value)}
                                            placeholder="••••••••"
                                        />
                                        {errors.password && <p className="text-sm text-red-500">{errors.password}</p>}
                                    </div>

                                    <div className="space-y-2">
                                        <Label htmlFor="password_confirmation" className="flex items-center gap-2">
                                            <Lock className="h-4 w-4" /> 確認新密碼
                                        </Label>
                                        <Input
                                            id="password_confirmation"
                                            type="password"
                                            value={data.password_confirmation}
                                            onChange={e => setData('password_confirmation', e.target.value)}
                                            placeholder="••••••••"
                                        />
                                    </div>
                                </div>
                            </div>
                        </div>

                        {/* Roles */}
                        <div className="md:col-span-1">
                            <div className="bg-white p-6 rounded-xl border border-gray-200 shadow-sm h-full">
                                <h3 className="font-bold text-gray-900 border-b pb-2 mb-4">角色分配</h3>
                                <div className="space-y-4">
                                    {roles.map((role) => (
                                        <div key={role.id} className="flex items-start space-x-3 p-2 hover:bg-gray-50 rounded-lg transition-colors">
                                            <Checkbox
                                                id={`role-${role.id}`}
                                                checked={data.roles.includes(role.name)}
                                                onCheckedChange={() => toggleRole(role.name)}
                                            // Prevent changing super-admin if user is editing themselves? Or just backend protection.
                                            />
                                            <div className="grid gap-1.5 leading-none">
                                                <label
                                                    htmlFor={`role-${role.id}`}
                                                    className="text-sm font-medium leading-none cursor-pointer"
                                                >
                                                    {translateRoleName(role.name)}
                                                </label>
                                                <p className="text-xs text-gray-500 font-mono">
                                                    {role.name}
                                                </p>
                                            </div>
                                        </div>
                                    ))}
                                    {errors.roles && <p className="text-sm text-red-500">{errors.roles}</p>}
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </AuthenticatedLayout>
    );
}
