package com.ponzischeme89.memby.update

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.ponzischeme89.memby.ui.MainActivity

/**
 * Reopens the normal app entry point when this package is replaced in place.
 *
 * The normal entry point runs the public update check and then reuses the existing saved
 * session. Nothing in this recovery path clears app data, profiles, or credentials.
 */
class UpdateRecoveryReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        if (intent.action != Intent.ACTION_MY_PACKAGE_REPLACED) return

        val launch = Intent(context, MainActivity::class.java).apply {
            addFlags(
            Intent.FLAG_ACTIVITY_NEW_TASK or
                Intent.FLAG_ACTIVITY_CLEAR_TOP or
                Intent.FLAG_ACTIVITY_SINGLE_TOP,
            )
        }
        runCatching { context.startActivity(launch) }
    }
}
