// Paso 3: BFS frente a Dijkstra entre pares de calles.
// Uso: compare_routes mapa.txt calles.tsv pares.tsv
#include <fstream>
#include <iomanip>
#include <iostream>
#include <random>
#include <string>
#include <vector>

#include "map_file.h"
#include "routes.h"
#include "traversals.h"

namespace {

void print_row(const Graph& graph, const MapFile& map, int from, int to, const std::string& label) {
    std::vector<int> fewest = path_to(bfs(graph, from).parent, to);
    DijkstraResult result = dijkstra_to(graph, from, to);
    std::vector<int> best = route_to(result.parent, to);
    std::cout << std::setw(8) << static_cast<long long>(straight_meters(map.points[from], map.points[to]))
              << std::setw(9) << fewest.size() - 1 << std::setw(9) << route_meters(graph, fewest) << std::setw(9)
              << best.size() - 1 << std::setw(9) << route_meters(graph, best) << "  " << label << "\n";
}

}  // namespace

int main(int argc, char* argv[]) {
    if (argc != 4) {
        std::cerr << "uso: compare_routes mapa.txt calles.tsv pares.tsv\n";
        return 2;
    }
    MapFile map = read_map_file(argv[1]);
    std::vector<Street> streets = read_streets(argv[2]);
    Graph graph = build_map_graph(map);
    std::cout << "              BFS               Dijkstra\n";
    std::cout << "   recta  aristas   metros  aristas   metros\n";
    std::ifstream pairs(argv[3]);
    std::string from;
    std::string to;
    while (std::getline(pairs, from, '\t') && std::getline(pairs, to)) {
        print_row(graph, map, node_of_street(streets, from), node_of_street(streets, to), from + " - " + to);
    }
    std::mt19937 random(15);
    for (int i = 0; i < 6; i++) {
        int source = static_cast<int>(random() % graph.node_count());
        int target = static_cast<int>(random() % graph.node_count());
        print_row(graph, map, source, target, "al azar");
    }
    return 0;
}
