-- Scale all town centers outward from their centroid (~1.45×) so inter-city distances grow
-- while preserving spiral layout. RoadGraph recomputes segment distances in Go; refresh waypoints.

UPDATE towns AS t SET
    world_x = c.cx + (t.world_x - c.cx) * 1.45,
    world_y = c.cy + (t.world_y - c.cy) * 1.45
FROM (SELECT AVG(world_x) AS cx, AVG(world_y) AS cy FROM towns) AS c;

DELETE FROM road_waypoints;

INSERT INTO road_waypoints (road_id, seq, x, y)
SELECT
    r.id,
    gs.seq,
    CASE
        WHEN gs.seq = 0 THEN f.world_x
        WHEN gs.seq = seg.nseg THEN t.world_x
        ELSE f.world_x + (t.world_x - f.world_x) * (gs.seq::double precision / seg.nseg::double precision)
    END,
    CASE
        WHEN gs.seq = 0 THEN f.world_y
        WHEN gs.seq = seg.nseg THEN t.world_y
        ELSE f.world_y + (t.world_y - f.world_y) * (gs.seq::double precision / seg.nseg::double precision)
    END
FROM roads r
INNER JOIN towns f ON f.id = r.from_town_id
INNER JOIN towns t ON t.id = r.to_town_id
CROSS JOIN LATERAL (
    SELECT GREATEST(
        1,
        FLOOR(
            SQRT(POWER(t.world_x - f.world_x, 2) + POWER(t.world_y - f.world_y, 2)) / 20.0
        )::integer
    ) AS nseg
) seg
CROSS JOIN LATERAL generate_series(0, seg.nseg) AS gs(seq);
