/*
 * Hermes Node Android
 * Copyright (C) 2026 Stefy Lanza <stefy@nexlab.net>
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
package net.nexlab.hermesnodeandroid

import android.content.Context
import org.json.JSONArray
import org.json.JSONObject

object NotificationSessionStore {
    private const val PREFS = "hermes_node_android_notifications"
    private const val KEY_LAST_SNAPSHOT = "last_snapshot"

    fun saveSnapshot(context: Context, payload: JSONArray) {
        context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).edit()
            .putString(KEY_LAST_SNAPSHOT, payload.toString())
            .apply()
    }

    fun loadSnapshot(context: Context): JSONArray? {
        val raw = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).getString(KEY_LAST_SNAPSHOT, null) ?: return null
        return runCatching { JSONArray(raw) }.getOrNull()
    }

    fun putStatus(context: Context, enabled: Boolean) {
        val current = JSONObject()
            .put("enabled", enabled)
            .put("updated_at", System.currentTimeMillis())
        context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).edit()
            .putString("listener_status", current.toString())
            .apply()
    }

    fun loadStatus(context: Context): JSONObject? {
        val raw = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).getString("listener_status", null) ?: return null
        return runCatching { JSONObject(raw) }.getOrNull()
    }
}
