package com.example.petstoremobile.viewmodels;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModel;

import com.example.petstoremobile.dtos.AuthDTO;
import com.example.petstoremobile.dtos.AvatarUploadResponse;
import com.example.petstoremobile.dtos.UserDTO;
import com.example.petstoremobile.repositories.AuthRepository;
import com.example.petstoremobile.utils.Resource;

import java.util.Map;

import javax.inject.Inject;

import dagger.hilt.android.lifecycle.HiltViewModel;
import okhttp3.MultipartBody;

@HiltViewModel
public class AuthViewModel extends ViewModel {
    private final AuthRepository repository;

    @Inject
    public AuthViewModel(AuthRepository repository) {
        this.repository = repository;
    }

    /**
     * Authenticates a user with username and password.
     */
    public LiveData<Resource<AuthDTO.LoginResponse>> login(String username, String password) {
        return repository.login(new AuthDTO.LoginRequest(username, password));
    }

    /**
     * Retrieves the profile information of the currently authenticated user.
     */
    public LiveData<Resource<UserDTO>> getMe() {
        return repository.getMe();
    }

    /**
     * Updates the profile information of the current user.
     */
    public LiveData<Resource<UserDTO>> updateMe(Map<String, String> updates) {
        return repository.updateMe(updates);
    }

    /**
     * Uploads a new avatar image for the current user.
     */
    public LiveData<Resource<AvatarUploadResponse>> uploadAvatar(MultipartBody.Part avatar) {
        return repository.uploadAvatar(avatar);
    }

    /**
     * Deletes the avatar image of the current user.
     */
    public LiveData<Resource<Void>> deleteAvatar() {
        return repository.deleteAvatar();
    }

    /**
     * Logs out the current user by clearing stored credentials.
     */
    public void logout() {
        repository.logout();
    }
}
