import { useMutation } from '@tanstack/react-query';

import { authQueryKeys } from '../auth-query-keys';
import { authApi, type PostRegisterUserReturn } from '../auth-api';

import type { BaseAxiosError } from '../../../../types';

export type UserRegistrationData = {
  user: {
    username: string;
    email: string;
    password: string;
    first_name: string;
    last_name: string;
    date_of_birth: string;
    homebase: number;
    homebase2?: number;
  };
  photo: {
    type: string | undefined;
    uri: string | undefined;
    name: string | undefined;
  };
};

export const useRegisterMutation = () => {
  return useMutation<
    PostRegisterUserReturn,
    BaseAxiosError,
    UserRegistrationData,
    PostRegisterUserReturn
  >({
    mutationKey: authQueryKeys.registerUser(),
    mutationFn: async (data) => {
      const response = await authApi.registerUser(data);
      return response.data;
    }
  });
};
