package com.isidroid.c23.domain.use_case

import android.content.Context
import com.isidroid.c23.data.mapper.createListItem
import com.isidroid.core.FlowResult
import com.isidroid.job.repository.JobRepository
import com.isidroid.spot.repository.SpotRepository
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.flow
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class PrintJobsUseCase @Inject constructor(
    @ApplicationContext private val context: Context,
    private val jobRepository: JobRepository,
    private val spotRepository: SpotRepository
) {
    fun load() = flow {
        emit(FlowResult.Loading)
        val jobList = jobRepository.readLocalList()
        val spots = jobList
            .map { it.spotId }
            .distinct()
            .let { spotRepository.findLocalRichSpots(it) }
            ?.associateBy({ it.spot.id }, { it })

        val result = jobList.map { job -> job.createListItem(context = context, richSpot = spots?.get(job.spotId)) }
        emit(FlowResult.Success(result))
    }

    fun findSpot(spotCode: String) = flow {
        emit(FlowResult.Loading)
        val result = spotRepository.findRichSpot(spotCode)?.spot
        emit(FlowResult.Success(result))
    }
}