#pragma once

// ── Tunnel pairing & coverage computation ─────────────────────────────────────
// Given the current list of placed pieces and tunnel-portal scenery items,
// pair up portals (1st+2nd, 3rd+4th, ...) and compute the set of pieces
// "inside the mountain" between each pair.
//
// Pairing rules:
//   * A portal anchors to its nearest piece, within 25 mm. Portals beyond
//     that radius are floating and cannot pair.
//   * Anchored portals on the same connected component of the joints graph
//     are ordered by walking the chain between them. The portal closer to
//     one end becomes the "entry," the next portal along the chain becomes
//     the "exit," and so on.
//   * Pairs form in placement order along the chain: (1,2), (3,4), etc.
//   * An odd-count run leaves the last portal unpaired.
//
// At a turnout the walker takes whichever leg leads to the next portal.
// If multiple portals are reachable through different legs, the closest
// portal (by edge count) wins.

#include <vector>
#include <string>
#include "model/PlacedPiece.h"
#include "model/PlacedScenery.h"

namespace MC {

struct TunnelPair {
    int portalIdA = -1;            // PlacedScenery.id of first portal
    int portalIdB = -1;            // PlacedScenery.id of second portal
    int anchorPieceA = -1;         // PlacedPiece.id of A's nearest piece
    int anchorPieceB = -1;         // PlacedPiece.id of B's nearest piece
    std::vector<int> coveredPieceIds;   // pieces (incl. endpoints) inside the tunnel
};

// Returns true if the given scenery item id is a tunnel portal.
bool IsTunnelPortalId(const std::string& itemId);

// Computes the current pairing. Inputs are read-only references; result lives
// in the returned vector. Stable: calling repeatedly with the same input
// returns the same pairs in the same order.
std::vector<TunnelPair> ComputeTunnelPairs(
    const std::vector<PlacedPiece>& pieces,
    const std::vector<PlacedScenery>& scenery);

// Convenience: flatten all covered piece IDs into a single sorted unique set
// (for fast "is this piece inside a tunnel?" lookups during rendering).
std::vector<int> CoveredPieceIds(const std::vector<TunnelPair>& pairs);

} // namespace MC
