use crate::ota::ota_manager;
use crate::ota::ota_update::OTAPlatform;
use crate::server::custom_error::HttpResult;
use actix_web::{HttpResponse, web};

#[derive(serde::Deserialize)]
pub struct FirmwarePath {
    platform: OTAPlatform,
    version: semver::Version,
}

/// Download firmware update
pub async fn retrieve_firmware(path: web::Path<FirmwarePath>) -> HttpResult {
    if !ota_manager::update_exists(path.platform, &path.version)? {
        return Ok(HttpResponse::NotFound().json("The requested firmware was not found!"));
    }

    let firmware = ota_manager::get_ota_update(path.platform, &path.version)?;

    Ok(HttpResponse::Ok().body(firmware))
}