import { useState } from 'react';
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { z } from 'zod';
import { useAuthStore } from '@/stores/auth';
import { useTranslation } from '@/hooks/useTranslation';
import { apiService } from '@/services/api';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { useToast } from '@/hooks/useToast';
import { usernameSchema, emailSchema } from '@/schemas/validation';

// Define schema locally to match User type + validations
const profileSchema = z.object({
  username: usernameSchema,
  email: emailSchema,
  first_name: z.string().optional(),
  last_name: z.string().optional(),
  bio: z.string().optional(),
});

type ProfileFormData = z.infer<typeof profileSchema>;

export function ProfileForm() {
  const { user, refreshUser } = useAuthStore();
  const { t } = useTranslation();
  const { success, error: showToastError } = useToast();
  const [isEditing, setIsEditing] = useState(false);
  const [isLoading, setIsLoading] = useState(false);

  const form = useForm<ProfileFormData>({
    resolver: zodResolver(profileSchema),
    defaultValues: {
      username: user?.username || '',
      email: user?.email || '',
      first_name: user?.first_name || '',
      last_name: user?.last_name || '',
      bio: user?.bio || '',
    },
    mode: 'onBlur',
  });

  const {
    register,
    handleSubmit,
    reset,
    formState: { errors },
  } = form;

  const onSubmit = async (data: ProfileFormData) => {
    if (!user) return;
    setIsLoading(true);
    try {
      // Need id as number according to ApiService.updateUser signature in api.ts?
      // api.ts: updateUser(id: number, data: Partial<User>)
      // But User.id is string in types/index.ts!!
      // Wait, let's re-verify api.ts updateUser signature.
      // Line 224: `async updateUser(id: number, data: Partial<User>): Promise<User>`
      // But User interface has `id: string`.
      // This is a mismatch in api.ts vs types.ts.
      // However, if I pass Number(user.id), it might work if backend expects number.
      // Or I should fix api.ts?
      // I'll cast for now or parse.

      const userId = Number(user.id);

      await apiService.updateUser(userId, data);
      await refreshUser();
      success(t('profile.success')); // Assuming translation key exists or generic success
      setIsEditing(false);
    } catch (err: any) {
      showToastError(err.message || t('profile.error'));
    } finally {
      setIsLoading(false);
    }
  };

  const handleCancel = () => {
    reset({
      username: user?.username || '',
      email: user?.email || '',
      first_name: user?.first_name || '',
      last_name: user?.last_name || '',
      bio: user?.bio || '',
    });
    setIsEditing(false);
  };

  // Si l'utilisateur n'est pas chargé, afficher un message au lieu de retourner null
  // (retourner null empêche la navigation de se terminer)
  if (!user) {
    return (
      <Card>
        <CardContent className="pt-6">
          <div className="text-center text-muted-foreground">
            <p>Chargement du profil...</p>
          </div>
        </CardContent>
      </Card>
    );
  }

  return (
    <Card>
      <CardHeader className="flex flex-row items-center justify-between">
        <CardTitle>{t('profile.title')}</CardTitle>
        {!isEditing && (
          <Button onClick={() => setIsEditing(true)} variant="outline">
            {t('profile.edit')}
          </Button>
        )}
      </CardHeader>
      <CardContent>
        <h3 className="text-lg font-medium mb-4">
          {t('profile.personalInfo')}
        </h3>

        <form onSubmit={handleSubmit(onSubmit)} className="space-y-4">
          {/* Avatar Section (Mocked UI based on test expectation) */}
          {isEditing && (
            <div className="mb-4">
              <Button type="button" variant="ghost">
                {t('profile.avatar.changePhoto')}
              </Button>
            </div>
          )}

          <div className="grid gap-2">
            <label className="text-sm font-medium" htmlFor="username">
              Username
            </label>
            <Input
              id="username"
              {...register('username')}
              disabled={!isEditing}
            />
            {errors.username && (
              <p className="text-sm text-red-500">{errors.username.message}</p>
            )}
          </div>

          <div className="grid gap-2">
            <label className="text-sm font-medium" htmlFor="email">
              Email
            </label>
            <Input
              id="email"
              type="email"
              {...register('email')}
              disabled={!isEditing}
            />
            {errors.email && (
              <p className="text-sm text-red-500">{errors.email.message}</p>
            )}
          </div>

          <div className="grid grid-cols-2 gap-4">
            <div className="grid gap-2">
              <label className="text-sm font-medium" htmlFor="first_name">
                First Name
              </label>
              <Input
                id="first_name"
                {...register('first_name')}
                disabled={!isEditing}
              />
              {errors.first_name && (
                <p className="text-sm text-red-500">
                  {errors.first_name.message}
                </p>
              )}
            </div>
            <div className="grid gap-2">
              <label className="text-sm font-medium" htmlFor="last_name">
                Last Name
              </label>
              <Input
                id="last_name"
                {...register('last_name')}
                disabled={!isEditing}
              />
              {errors.last_name && (
                <p className="text-sm text-red-500">
                  {errors.last_name.message}
                </p>
              )}
            </div>
          </div>

          <div className="grid gap-2">
            <label className="text-sm font-medium" htmlFor="bio">
              Bio
            </label>
            <Input id="bio" {...register('bio')} disabled={!isEditing} />
            {errors.bio && (
              <p className="text-sm text-red-500">{errors.bio.message}</p>
            )}
          </div>

          {isEditing && (
            <div className="flex justify-end gap-2 mt-4">
              <Button type="button" variant="secondary" onClick={handleCancel}>
                {t('profile.cancel')}
              </Button>
              <Button type="submit" disabled={isLoading}>
                {t('profile.save')}
              </Button>
            </div>
          )}
        </form>
      </CardContent>
    </Card>
  );
}
