-- SQL Script to add a test event and stages for production testing of the /stages/live endpoint.
-- This script is designed for MySQL.
--
-- IMPORTANT: Before running, replace 'REPLACE_WITH_A_VALID_USER_UUID' with a real user ID from your production database.

-- Step 1: Define UUIDs for the new records.
-- You can generate new ones using an online tool or MySQL's UUID() function if you prefer.
SET @event_id = 'a1b2c3d4-e5f6-7890-1234-567890abcdef';
SET @live_stage_id = 'b2c3d4e5-f6a7-8901-2345-67890abcdef1';
SET @future_stage_id = 'c3d4e5f6-a7b8-9012-3456-7890abcdef12';

-- Step 2: Insert the test event.
-- This event is set to be currently active to ensure its stages can be "live".
INSERT INTO `events` (`id`, `name`, `description`, `start_time`, `end_time`, `postal_code`, `city`, `country`, `organiser_id`)
VALUES (
    @event_id,
    'Live Location API Test Event',
    'An event created specifically to test the live stage location endpoint in production.',
    NOW() - INTERVAL 1 DAY,  -- Starts yesterday
    NOW() + INTERVAL 1 DAY,  -- Ends tomorrow
    '75001',
    'Paris',
    'France',
    'REPLACE_WITH_A_VALID_USER_UUID' -- <-- IMPORTANT: REPLACE THIS VALUE
);

-- Step 3: Insert a "live" stage for the event.
-- This stage has a GPX file and its start/end times are set to make it "live" right now.
-- The GPX file path corresponds to the one used in testing, located near lat=48.155, lon=-1.691
INSERT INTO `stages` (`id`, `event_id`, `name`, `description`, `start_time`, `end_time`, `gpx_file_url`, `min_age`)
VALUES (
    @live_stage_id,
    @event_id,
    'Nearby Live Test Stage (Should be Found)',
    'This stage should be found by the location search when querying with coordinates from the GPX file.',
    NOW() - INTERVAL 1 HOUR, -- Starts an hour ago
    NOW() + INTERVAL 1 HOUR, -- Ends in an hour
    'gpx/stage_0.gpx',
    0
);

-- Step 4: Insert a non-live stage for the same event (optional).
-- This stage has start/end times in the future, so it should NOT be found by the live search.
INSERT INTO `stages` (`id`, `event_id`, `name`, `description`, `start_time`, `end_time`, `gpx_file_url`, `min_age`)
VALUES (
    @future_stage_id,
    @event_id,
    'Future Test Stage (Should NOT be Found)',
    'This stage is in the future and should not be found by the live endpoint.',
    NOW() + INTERVAL 2 DAY, -- Starts in 2 days
    NOW() + INTERVAL 3 DAY, -- Ends in 3 days
    'gpx/stage_0.gpx',
    0
);

SELECT 'Test data created successfully.' as 'Status';
SELECT CONCAT('Test Event ID: ', @event_id) as 'Details';
SELECT 'You can now test the endpoint using a URL like:';
SELECT CONCAT('/api/v1/events/', @event_id, '/stages/live?lat=48.155&long=-1.691&tolerance=10000') as 'Example URL';
