/*
 * 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

object StatusStore {
    const val ACTION_STATUS_CHANGED = "net.nexlab.hermesnodeandroid.STATUS_CHANGED"
    private const val PREFS = "hermes_node_android_status"

    fun set(context: Context, status: String, detail: String = "") {
        context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).edit()
            .putString("status", status)
            .putString("detail", detail)
            .putLong("updated_at", System.currentTimeMillis())
            .apply()
        context.sendBroadcast(android.content.Intent(ACTION_STATUS_CHANGED))
    }

    fun snapshot(context: Context): String {
        val prefs = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
        val status = prefs.getString("status", "stopped") ?: "stopped"
        val detail = prefs.getString("detail", "") ?: ""
        val updated = prefs.getLong("updated_at", 0L)
        return if (detail.isBlank()) "$status\nupdated: $updated" else "$status\n$detail\nupdated: $updated"
    }
}
