package app.closer.data.repository

import android.app.Activity
import android.app.Application
import android.os.Bundle

/**
 * Tracks the resumed Activity so that RevenueCat purchase calls can obtain an
 * Activity reference without lifecycle-aware components in the repository layer.
 *
 * Registered once by the Application instance. This is a minimal, process-global
 * tracker; only safe because purchases are triggered from a foreground screen.
 */
object ActivityProvider : Application.ActivityLifecycleCallbacks {

    @Volatile
    private var _currentActivity: Activity? = null

    val currentActivity: Activity? get() = _currentActivity

    fun register(application: Application) {
        application.registerActivityLifecycleCallbacks(this)
    }

    override fun onActivityResumed(activity: Activity) {
        _currentActivity = activity
    }

    override fun onActivityPaused(activity: Activity) {
        if (_currentActivity === activity) {
            _currentActivity = null
        }
    }

    override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) = Unit
    override fun onActivityStarted(activity: Activity) = Unit
    override fun onActivityStopped(activity: Activity) = Unit
    override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) = Unit
    override fun onActivityDestroyed(activity: Activity) = Unit
}
