#pragma once
#include "Vec2.h"

namespace MC {

// A connection point on a track piece.
// pos    — offset from the piece's local origin (mm)
// heading — direction a train exits this endpoint (degrees, 0=east)
//
// Two endpoints are compatible if their world-space headings are
// exactly 180° apart (one exits where the other enters).
struct ConnectionPoint {
    Vec2   pos;
    double heading  = 0.0;   // local, relative to piece origin
    bool   isOpen   = true;  // not yet joined to another piece
    int    index    = -1;    // index within parent piece's connection list

    ConnectionPoint() = default;
    ConnectionPoint(Vec2 pos, double heading, int index = -1)
        : pos(pos), heading(heading), index(index) {}

    // Are two world-space headings compatible for joining?
    // They must point in opposite directions (within tolerance).
    static bool headingsCompatible(double h1, double h2, double tolDeg = 1.0) {
        double diff = std::abs(std::fmod(std::abs(h1 - h2), 360.0) - 180.0);
        return diff <= tolDeg;
    }
};

} // namespace MC
