/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "cuttlefish/host/libs/metrics/guest_metrics.h"

#include <string>
#include <vector>

#include "fmt/format.h"

#include "cuttlefish/host/commands/assemble_cvd/boot_image_utils.h"
#include "cuttlefish/result/result.h"

namespace cuttlefish {

Result<std::vector<GuestMetrics>> GetGuestMetrics(const Guests& guests) {
  std::vector<GuestMetrics> result;
  result.reserve(guests.guest_infos.size());

  for (int i = 0; i < guests.guest_infos.size(); i++) {
    const std::string boot_image_path =
        fmt::format("{}/boot.img", guests.guest_infos[i].product_out);
    result.emplace_back(GuestMetrics{
        .instance_id = guests.guest_infos[i].instance_id,
        .os_version = CF_EXPECTF(
            ReadAndroidVersionFromBootImage(boot_image_path),
            "Failed to read guest os version from '{}'.", boot_image_path),
        .event_type = guests.event_type,
        .flag_metrics = CF_EXPECT(GetFlagMetrics(guests.parsed_flags, i)),
    });
  }
  return result;
}

}  // namespace cuttlefish
