package app.closer.core.notifications

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import app.closer.MainActivity
import app.closer.R

object NotificationHelper {

    const val CHANNEL_REMINDERS = "reminders"
    const val CHANNEL_PARTNER = "partner_activity"
    const val CHANNEL_GAME_ACTIVITY = "game_activity"

    fun createChannels(context: Context) {
        val nm = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        nm.createNotificationChannel(
            NotificationChannel(
                CHANNEL_REMINDERS,
                "Daily reminders",
                NotificationManager.IMPORTANCE_DEFAULT
            ).apply {
                description = "Gentle nudges for today's question and shared moments"
            }
        )
        nm.createNotificationChannel(
            NotificationChannel(
                CHANNEL_PARTNER,
                "Partner activity",
                NotificationManager.IMPORTANCE_HIGH
            ).apply {
                description = "When your partner answers, reveals, or opens a conversation"
            }
        )
        nm.createNotificationChannel(
            NotificationChannel(
                CHANNEL_GAME_ACTIVITY,
                "Games",
                NotificationManager.IMPORTANCE_HIGH
            ).apply {
                description = "When your partner starts or completes a game round"
            }
        )
    }

    fun show(context: Context, id: Int, channelId: String, title: String, body: String) {
        val intent = Intent(context, MainActivity::class.java).apply {
            flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
        }
        val pending = PendingIntent.getActivity(
            context, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
        )
        val notification = NotificationCompat.Builder(context, channelId)
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setContentTitle(title)
            .setContentText(body)
            .setAutoCancel(true)
            .setContentIntent(pending)
            .build()

        if (NotificationManagerCompat.from(context).areNotificationsEnabled()) {
            NotificationManagerCompat.from(context).notify(id, notification)
        }
    }
}
