package com.devlab.app.util

import android.app.NotificationManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class NotificationActionReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        if (intent.action == "com.devlab.app.MARK_READ") {
            val notificationId = intent.getIntExtra("notification_id", 0)
            if (notificationId > 0) {
                val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
                manager.cancel(notificationId)

                CoroutineScope(Dispatchers.IO).launch {
                    try {
                        val service = com.devlab.app.data.api.RetrofitClient.getApiService()
                        service.markNotificationRead(notificationId)
                    } catch (_: Exception) { }
                }
            }
        }
    }
}
