package app.closer.data.repository

import app.closer.core.crash.CrashReporter
import app.closer.data.remote.FirestoreOutcomeDataSource
import app.closer.domain.model.Outcome
import app.closer.domain.model.OutcomeDayKey
import app.closer.domain.model.OutcomeScores
import app.closer.domain.repository.OutcomeRepository
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class OutcomeRepositoryImpl @Inject constructor(
    private val dataSource: FirestoreOutcomeDataSource,
    private val crashReporter: CrashReporter
) : OutcomeRepository {

    override suspend fun submitOutcome(
        coupleId: String,
        dayKey: OutcomeDayKey,
        scores: OutcomeScores
    ): Result<Unit> = runCatching {
        dataSource.submitOutcome(coupleId, dayKey, scores)
    }.onFailure { crashReporter.recordException(it) }

    override suspend fun getOutcomes(coupleId: String): List<Outcome> =
        runCatching { dataSource.getOutcomes(coupleId) }
            .onFailure { crashReporter.recordException(it) }
            .getOrDefault(emptyList())

    override suspend fun getOutcome(coupleId: String, dayKey: OutcomeDayKey): Outcome? =
        runCatching { dataSource.getOutcome(coupleId, dayKey) }
            .onFailure { crashReporter.recordException(it) }
            .getOrNull()
}
