-- Migration 000027: Cross-roads — add shortcut roads between non-adjacent towns
-- so that from some towns there are multiple destination choices.

-- Shortcut 1: Willowdale <-> Ashengard (bypasses Mossharbor + Thornwatch + Emberwell)
INSERT INTO roads (from_town_id, to_town_id, distance)
SELECT f.id, t.id, 1500.0
FROM towns f, towns t
WHERE f.name = 'Willowdale' AND t.name = 'Ashengard'
  AND NOT EXISTS (SELECT 1 FROM roads r WHERE r.from_town_id = f.id AND r.to_town_id = t.id);

INSERT INTO roads (from_town_id, to_town_id, distance)
SELECT f.id, t.id, 1500.0
FROM towns f, towns t
WHERE f.name = 'Ashengard' AND t.name = 'Willowdale'
  AND NOT EXISTS (SELECT 1 FROM roads r WHERE r.from_town_id = f.id AND r.to_town_id = t.id);

-- Shortcut 2: Thornwatch <-> Frostmark (bypasses Emberwell + Ashengard)
INSERT INTO roads (from_town_id, to_town_id, distance)
SELECT f.id, t.id, 1200.0
FROM towns f, towns t
WHERE f.name = 'Thornwatch' AND t.name = 'Frostmark'
  AND NOT EXISTS (SELECT 1 FROM roads r WHERE r.from_town_id = f.id AND r.to_town_id = t.id);

INSERT INTO roads (from_town_id, to_town_id, distance)
SELECT f.id, t.id, 1200.0
FROM towns f, towns t
WHERE f.name = 'Frostmark' AND t.name = 'Thornwatch'
  AND NOT EXISTS (SELECT 1 FROM roads r WHERE r.from_town_id = f.id AND r.to_town_id = t.id);

-- Shortcut 3: Redcliff <-> Cinderkeep (bypasses Duskwatch + Boghollow)
INSERT INTO roads (from_town_id, to_town_id, distance)
SELECT f.id, t.id, 1400.0
FROM towns f, towns t
WHERE f.name = 'Redcliff' AND t.name = 'Cinderkeep'
  AND NOT EXISTS (SELECT 1 FROM roads r WHERE r.from_town_id = f.id AND r.to_town_id = t.id);

INSERT INTO roads (from_town_id, to_town_id, distance)
SELECT f.id, t.id, 1400.0
FROM towns f, towns t
WHERE f.name = 'Cinderkeep' AND t.name = 'Redcliff'
  AND NOT EXISTS (SELECT 1 FROM roads r WHERE r.from_town_id = f.id AND r.to_town_id = t.id);

-- Shortcut 4: Mossharbor <-> Emberwell (bypasses Thornwatch)
INSERT INTO roads (from_town_id, to_town_id, distance)
SELECT f.id, t.id, 1100.0
FROM towns f, towns t
WHERE f.name = 'Mossharbor' AND t.name = 'Emberwell'
  AND NOT EXISTS (SELECT 1 FROM roads r WHERE r.from_town_id = f.id AND r.to_town_id = t.id);

INSERT INTO roads (from_town_id, to_town_id, distance)
SELECT f.id, t.id, 1100.0
FROM towns f, towns t
WHERE f.name = 'Emberwell' AND t.name = 'Mossharbor'
  AND NOT EXISTS (SELECT 1 FROM roads r WHERE r.from_town_id = f.id AND r.to_town_id = t.id);

-- Generate waypoints for the new cross-roads (same rule as migration 000019).
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
LEFT JOIN road_waypoints rw ON rw.road_id = r.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)
WHERE rw.road_id IS NULL;
