import { useState } from 'react';
import { authApi } from '@/services/api/auth';
import type { ForgotPasswordFormData, ResetPasswordFormData } from '../types';

export function usePasswordReset() {
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<Error | null>(null);
  const [success, setSuccess] = useState(false);

  const handleRequestReset = async (data: ForgotPasswordFormData) => {
    try {
      setLoading(true);
      setError(null);
      setSuccess(false);
      await requestPasswordReset(data);
      setSuccess(true);
    } catch (err) {
      setError(err as Error);
      setSuccess(false);
    } finally {
      setLoading(false);
    }
  };

  const handleReset = async (data: ResetPasswordFormData) => {
    try {
      setLoading(true);
      setError(null);
      setSuccess(false);
      await authApi.resetPassword(data);
      setSuccess(true);
    } catch (err) {
      setError(err as Error);
      setSuccess(false);
    } finally {
      setLoading(false);
    }
  };

  return {
    handleRequestReset,
    handleReset,
    loading,
    error,
    success,
  };
}
