/*
 * Copyright (c) 2026 Meshtastic LLC
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
package org.meshtastic.core.data.manager

import co.touchlab.kermit.Logger
import org.koin.core.annotation.Single
import org.meshtastic.core.repository.NeighborInfoHandler
import org.meshtastic.core.repository.NodeManager
import org.meshtastic.core.repository.NodeRepository
import org.meshtastic.core.repository.ServiceStateWriter
import org.meshtastic.proto.MeshPacket
import org.meshtastic.proto.NeighborInfo

@Single
class NeighborInfoHandlerImpl(
    private val nodeManager: NodeManager,
    private val serviceStateWriter: ServiceStateWriter,
    private val nodeRepository: NodeRepository,
) : NeighborInfoHandler {

    private val requestTimer = RequestTimer()

    override var lastNeighborInfo: NeighborInfo? = null

    override fun recordStartTime(requestId: Int) = requestTimer.start(requestId)

    override fun handleNeighborInfo(packet: MeshPacket) {
        val payload = packet.decoded?.payload ?: return
        val ni = NeighborInfo.ADAPTER.decode(payload)

        // Store the last neighbor info from our connected radio
        val from = packet.from
        if (from == nodeManager.myNodeNum.value) {
            lastNeighborInfo = ni
            Logger.d { "Stored last neighbor info from connected radio" }
        }

        // Format for UI response
        val requestId = packet.decoded?.request_id ?: 0

        val neighbors =
            ni.neighbors.joinToString("\n") { n ->
                val user = nodeRepository.getUser(n.node_id)
                val name = "${user.long_name} (${user.short_name})"
                "• $name (SNR: ${n.snr})"
            }

        val fromUser = nodeRepository.getUser(from)
        val formatted = "Neighbors of ${fromUser.long_name}:\n$neighbors"

        val responseText = requestTimer.appendDuration(requestId, formatted, "Neighbor info")

        serviceStateWriter.setNeighborInfoResponse(responseText)
    }
}
