package com.prodhack.moscow2025.domain.usecase.auth

import android.util.Log
import com.prodhack.moscow2025.domain.interfaces.AuthRepository
import com.prodhack.moscow2025.domain.interfaces.UserRepository
import kotlinx.coroutines.flow.firstOrNull
import org.koin.core.annotation.Single

enum class SessionState {
	NotAuthorized,
	NotFilledProfile,
	FilledAndAuthorized
}

@Single
class CheckSessionUseCase(
	private val authRepository: AuthRepository,
	private val userRepository: UserRepository
) {

	private companion object {
		const val TAG = "CheckSessionUseCase"
	}

	/**
	 * @return session state in enum format [SessionState]
	 */
	suspend operator fun invoke(): SessionState =
		if (authRepository.fetchLoginState().firstOrNull() == true) {
			Log.d(TAG, "user authorized, requesting profile")
			if (userRepository.fetchProfile().getOrNull()?.firstName.isNullOrBlank()) {
				Log.d(TAG, "user authorized, first name is blank -> need fill profile")
				SessionState.NotFilledProfile
			} else {
				Log.d(TAG, "user authorized, first name is filled -> user already fill profile")
				SessionState.FilledAndAuthorized
			}
		} else {
			SessionState.NotAuthorized
		}
}
