-- 📍 Tuvasta lõigud, mis geomeetriliselt lõikuvad, aga pole topoloogiliselt ühendatud
-- (st. ei jaga sama source/target)

CREATE TABLE IF NOT EXISTS :schema.intersections_unconnected AS
SELECT 
  l1.id AS id1,
  l2.id AS id2,
  ST_Intersection(l1.geom, l2.geom) AS intersection_geom
FROM :schema.links l1
JOIN :schema.links l2
  ON ST_Intersects(l1.geom, l2.geom)
WHERE l1.id < l2.id
  AND NOT (
    l1.source = l2.source OR l1.source = l2.target OR
    l1.target = l2.source OR l1.target = l2.target
  )
  AND GeometryType(ST_Intersection(l1.geom, l2.geom)) = 'POINT';

-- ℹ️ Vaata tulemusi:
-- SELECT * FROM :schema.intersections_unconnected;
