-- Clear existing test data
TRUNCATE TABLE eli_test.segment2 RESTART IDENTITY;

-- Insert comprehensive test data with all required columns
INSERT INTO eli_test.segment2
  (id_0, mapid, segment_type, segment_status, id, protection_status, qc_flag, group_1, type, length, cost, fdh_id, geom)
VALUES
-- Zone_A segments (mapid = 1001)
(1, 1001, 'Aerial', 'Proposed', 101, 'Protected', NULL, 'Zone_A', 'Aerial', 150.5, 1500.00, 1,
 ST_Transform(ST_GeomFromText('LINESTRING(-122.4194 37.7749, -122.4184 37.7759)', 4326), 6561)),

(2, 1001, 'Aerial', 'Proposed', 102, 'Protected', NULL, 'Zone_A', 'Aerial', 145.2, 1450.00, 1,
 ST_Transform(ST_GeomFromText('LINESTRING(-122.4184 37.7759, -122.4174 37.7769)', 4326), 6561)),

(3, 1001, 'Aerial', 'Proposed', 103, 'Protected', NULL, 'Zone_A', 'Aerial', 148.8, 1480.00, 1,
 ST_Transform(ST_GeomFromText('LINESTRING(-122.4174 37.7769, -122.4164 37.7779)', 4326), 6561)),

(4, 1001, 'Underground', 'Proposed', 104, 'Protected', NULL, 'Zone_A', 'Underground', 142.3, 2850.00, 1,
 ST_Transform(ST_GeomFromText('LINESTRING(-122.4164 37.7779, -122.4154 37.7789)', 4326), 6561)),

(5, 1001, 'Underground', 'Proposed', 105, 'Protected', NULL, 'Zone_A', 'Underground', 138.7, 2775.00, 1,
 ST_Transform(ST_GeomFromText('LINESTRING(-122.4154 37.7789, -122.4144 37.7799)', 4326), 6561)),

(6, 1001, 'Aerial', 'Proposed', 106, 'Unprotected', NULL, 'Zone_A', 'Aerial', 155.0, 1550.00, 1,
 ST_Transform(ST_GeomFromText('LINESTRING(-122.4200 37.7800, -122.4210 37.7810)', 4326), 6561)),

-- Long span for single-span testing
(7, 1001, 'Aerial', 'Proposed', 107, 'Protected', NULL, 'Zone_A', 'Aerial', 2200.0, 22000.00, 1,
 ST_Transform(ST_GeomFromText('LINESTRING(-122.4220 37.7750, -122.4280 37.7760)', 4326), 6561)),

(8, 1001, 'Underground', 'Proposed', 108, 'Protected', NULL, 'Zone_A', 'Underground', 15.5, 310.00, 1,
 ST_Transform(ST_GeomFromText('LINESTRING(-122.4250 37.7820, -122.4249 37.7821)', 4326), 6561)),

-- Disconnected/isolated segment
(9, 1001, 'Aerial', 'Proposed', 109, 'Protected', NULL, 'Zone_A', 'Aerial', 140.0, 1400.00, 1,
 ST_Transform(ST_GeomFromText('LINESTRING(-122.5000 37.8000, -122.4990 37.8010)', 4326), 6561)),

-- Branching segments
(10, 1001, 'Aerial', 'Proposed', 110, 'Protected', NULL, 'Zone_A', 'Aerial', 145.0, 1450.00, 1,
 ST_Transform(ST_GeomFromText('LINESTRING(-122.4140 37.7730, -122.4130 37.7740)', 4326), 6561)),

(11, 1001, 'Aerial', 'Proposed', 111, 'Protected', NULL, 'Zone_A', 'Aerial', 142.0, 1420.00, 1,
 ST_Transform(ST_GeomFromText('LINESTRING(-122.4130 37.7740, -122.4120 37.7750)', 4326), 6561)),

(12, 1001, 'Aerial', 'Proposed', 112, 'Protected', NULL, 'Zone_A', 'Aerial', 144.0, 1440.00, 1,
 ST_Transform(ST_GeomFromText('LINESTRING(-122.4130 37.7740, -122.4120 37.7730)', 4326), 6561)),

(13, 1001, 'Aerial', 'Constructed', 113, 'Protected', NULL, 'Zone_A', 'Aerial', 152.0, 1520.00, 1,
 ST_Transform(ST_GeomFromText('LINESTRING(-122.4340 37.7680, -122.4330 37.7690)', 4326), 6561)),

(14, 1001, 'Underground', 'Design', 114, 'Unprotected', NULL, 'Zone_A', 'Underground', 139.5, 2790.00, 1,
 ST_Transform(ST_GeomFromText('LINESTRING(-122.4330 37.7690, -122.4320 37.7700)', 4326), 6561)),

(15, 1001, 'Aerial', 'Proposed', 115, 'Protected', NULL, 'Zone_A', 'Aerial', 160.0, 1600.00, 1,
 ST_Transform(ST_GeomFromText('LINESTRING(-122.4300 37.7700, -122.4100 37.7700)', 4326), 6561)),

-- Zone_B segments (mapid = 1002)
(16, 1002, 'Aerial', 'Existing', 201, 'Protected', NULL, 'Zone_B', 'Aerial', 147.5, 1475.00, 2,
 ST_Transform(ST_GeomFromText('LINESTRING(-122.4094 37.7849, -122.4084 37.7859)', 4326), 6561)),

(17, 1002, 'Underground', 'Existing', 202, 'Protected', NULL, 'Zone_B', 'Underground', 143.2, 2865.00, 2,
 ST_Transform(ST_GeomFromText('LINESTRING(-122.4084 37.7859, -122.4074 37.7869)', 4326), 6561)),

(18, 1002, 'Aerial', 'Proposed', 203, 'Protected', NULL, 'Zone_B', 'Aerial', 149.9, 1499.00, 2,
 ST_Transform(ST_GeomFromText('LINESTRING(-122.4074 37.7869, -122.4064 37.7879)', 4326), 6561)),

-- Zone_C segments (mapid = 1003)
(19, 1003, 'Aerial', 'Proposed', 301, 'Protected', NULL, 'Zone_C', 'Aerial', 146.3, 1463.00, 3,
 ST_Transform(ST_GeomFromText('LINESTRING(-122.3994 37.7949, -122.3984 37.7959)', 4326), 6561)),

(20, 1003, 'Underground', 'Proposed', 302, 'Protected', NULL, 'Zone_C', 'Underground', 141.8, 2836.00, 3,
 ST_Transform(ST_GeomFromText('LINESTRING(-122.3984 37.7959, -122.3974 37.7969)', 4326), 6561));

-- Add corresponding market entries to map_projects if they don't exist
INSERT INTO eli_test.map_projects (mapid, project)
SELECT 1001, 'Test Market A'
WHERE NOT EXISTS (SELECT 1 FROM eli_test.map_projects WHERE mapid = 1001)
UNION ALL
SELECT 1002, 'Test Market B'
WHERE NOT EXISTS (SELECT 1 FROM eli_test.map_projects WHERE mapid = 1002)
UNION ALL
SELECT 1003, 'Test Market C'
WHERE NOT EXISTS (SELECT 1 FROM eli_test.map_projects WHERE mapid = 1003);

-- Verify the data
SELECT COUNT(*) as total_segments FROM eli_test.segment2;
SELECT mapid, group_1, COUNT(*) as segment_count
FROM eli_test.segment2
GROUP BY mapid, group_1
ORDER BY mapid, group_1;
