package com.radiola.domain.usecase.auth

import com.radiola.domain.repository.FavoritesRepository
import com.radiola.domain.repository.SyncRepository
import kotlinx.coroutines.flow.first
import javax.inject.Inject

class SyncFavoritesUseCase @Inject constructor(
    private val favoritesRepository: FavoritesRepository,
    private val syncRepository: SyncRepository
) {
    suspend operator fun invoke() {
        syncRepository.fetchRemoteFavorites()
            .onSuccess { remoteIds ->
                val localIds = favoritesRepository.getFavoriteIds().first()
                // Add remote favorites that are missing locally
                remoteIds.forEach { id ->
                    if (id !in localIds) {
                        favoritesRepository.addFavorite(id)
                    }
                }
            }
    }
}
