package app.closer.domain.repository

import com.revenuecat.purchases.Package
import com.revenuecat.purchases.CustomerInfo
import com.revenuecat.purchases.Offerings
import com.revenuecat.purchases.PurchaseResult
import kotlinx.coroutines.flow.Flow

/**
 * Billing repository backed by RevenueCat.
 *
 * Responsibilities:
 * - Fetch RevenueCat [Offerings] for the current user
 * - Initiate purchase of a [Package]
 * - Restore previous purchases
 * - Expose [CustomerInfo] updates as a reactive stream
 *
 * All operations emit structured [BillingState] values so the UI can render
 * loading, success, error, and purchase-completed states consistently.
 */
interface BillingRepository {

    /** Fetch current RevenueCat offerings. */
    suspend fun getOfferings(): BillingState<Offerings>

    /** Purchase the supplied RevenueCat package. */
    suspend fun purchasePackage(pkg: Package): BillingState<PurchaseResult>

    /** Restore previous purchases for this device/account. */
    suspend fun restorePurchases(): BillingState<CustomerInfo>

    /** Continuous stream of customer info updates. */
    fun getCustomerInfo(): Flow<BillingState<CustomerInfo>>
}

/**
 * Sealed representation of a billing operation result.
 */
sealed interface BillingState<out T> {
    data object Loading : BillingState<Nothing>
    data class Success<T>(val data: T) : BillingState<T>
    data class Error(val message: String) : BillingState<Nothing>
}
