#include "torlinkc/engine/reconcile.hpp"

#include <unordered_set>

namespace torlinkc {

std::vector<QueueItem> reconcileQueue(const std::vector<QueueItem>& items) {
  std::unordered_set<std::string> seen;
  std::vector<QueueItem> out;
  out.reserve(items.size());
  for (const auto& raw : items) {
    if (raw.id.empty() || seen.count(raw.id)) continue;
    seen.insert(raw.id);
    if (raw.status == DownloadStatus::Completed) continue;

    QueueItem it = raw;
    switch (raw.status) {
      case DownloadStatus::Failed:
      case DownloadStatus::Paused:
      case DownloadStatus::Queued:
        break;  // preserved as-is
      default:
        it.status = DownloadStatus::Downloading;
        break;
    }
    it.speed = 0;
    it.peers = 0;
    it.eta.reset();
    out.push_back(std::move(it));
  }
  return out;
}

}  // namespace torlinkc
