/*
 * 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.feature.firmware

import org.koin.core.annotation.Single
import org.meshtastic.core.common.util.CommonUri
import org.meshtastic.core.database.entity.FirmwareRelease
import org.meshtastic.core.model.DeviceHardware
import org.meshtastic.core.repository.NodeRepository
import org.meshtastic.core.repository.RadioController

/** Handles firmware updates via USB Mass Storage (UF2). */
@Single
class UsbUpdateHandler(
    private val firmwareRetriever: FirmwareRetriever,
    private val radioController: RadioController,
    private val nodeRepository: NodeRepository,
) : FirmwareUpdateHandler {
    override suspend fun startUpdate(
        release: FirmwareRelease,
        hardware: DeviceHardware,
        target: String,
        updateState: (FirmwareUpdateState) -> Unit,
        firmwareUri: CommonUri?,
    ): FirmwareArtifact? = performUsbUpdate(
        release = release,
        hardware = hardware,
        firmwareUri = firmwareUri,
        radioController = radioController,
        nodeRepository = nodeRepository,
        updateState = updateState,
        retrieveUsbFirmware = firmwareRetriever::retrieveUsbFirmware,
    )
}
