'use client';

import { useCallback, useState } from 'react';

import { apiAccounts } from '../../clients';
import { authLogger } from '../utils/logger';
import { useAuth } from '../context';

export interface DeleteAccountResult {
  success: boolean;
  message: string;
}

export interface UseDeleteAccountReturn {
  /** Loading state */
  isLoading: boolean;
  /** Error message */
  error: string | null;
  /** Delete the account */
  deleteAccount: () => Promise<DeleteAccountResult>;
  /** Clear error */
  clearError: () => void;
}

/**
 * Hook for deleting user account.
 *
 * This performs a soft delete - deactivates the account and anonymizes personal data.
 * The account can be restored by an administrator if needed.
 *
 * @example
 * ```tsx
 * const { deleteAccount, isLoading, error } = useDeleteAccount();
 *
 * const handleDelete = async () => {
 *   const result = await deleteAccount();
 *   if (result.success) {
 *     // Account deleted, perform logout
 *     await logout();
 *   }
 * };
 * ```
 */
export const useDeleteAccount = (): UseDeleteAccountReturn => {
  const [isLoading, setIsLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const { logout } = useAuth();

  const clearError = useCallback(() => {
    setError(null);
  }, []);

  /**
   * Delete the user account (soft delete)
   */
  const deleteAccount = useCallback(async (): Promise<DeleteAccountResult> => {
    setIsLoading(true);
    setError(null);

    try {
      authLogger.info('Deleting account...');

      const response = await apiAccounts.user_profile.accountsProfileDeleteCreate();

      if (!response.success) {
        authLogger.error('Failed to delete account:', response.message);
        setError(response.message);
        throw new Error(response.message);
      }

    } catch (error) {
      authLogger.error('Failed to delete account:', error);
      throw error;
    } finally {
      setIsLoading(false);
    }

    // Return success response

    await logout();
    return { success: true, message: 'Account deleted successfully' };
  }, []);

  return {
    isLoading,
    error,
    deleteAccount,
    clearError,
  };
};
