-- Add start location fields to trips table
ALTER TABLE trips
ADD COLUMN IF NOT EXISTS start_location_type TEXT CHECK (start_location_type IN ('balloon', 'hotel', 'place', 'city_center')),
ADD COLUMN IF NOT EXISTS start_lat DOUBLE PRECISION,
ADD COLUMN IF NOT EXISTS start_lng DOUBLE PRECISION;

-- Add fixed time fields to places table
ALTER TABLE places
ADD COLUMN IF NOT EXISTS is_time_fixed BOOLEAN DEFAULT false,
ADD COLUMN IF NOT EXISTS fixed_start_time TIME,
ADD COLUMN IF NOT EXISTS fixed_end_time TIME;

-- Create index for faster queries on time-fixed places
CREATE INDEX IF NOT EXISTS idx_places_time_fixed ON places(is_time_fixed) WHERE is_time_fixed = true;

-- Update balloon flight places to be time-fixed (sunrise: 05:30-07:30)
UPDATE places
SET
  is_time_fixed = true,
  fixed_start_time = '05:30:00',
  fixed_end_time = '07:30:00'
WHERE type = 'hot_air_balloon' OR name ILIKE '%balon%' OR name ILIKE '%balloon%';

-- Add requires_sunrise to ai_daily_tour_rules if table exists
DO $$
BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'ai_daily_tour_rules') THEN
    ALTER TABLE ai_daily_tour_rules
    ADD COLUMN IF NOT EXISTS requires_sunrise BOOLEAN DEFAULT false;
    
    -- Mark balloon_day tour as requiring sunrise
    UPDATE ai_daily_tour_rules
    SET requires_sunrise = true
    WHERE daily_tour_slug = 'balloon_day';
  END IF;
END $$;

-- Add comment for documentation
COMMENT ON COLUMN trips.start_location_type IS 'Rota başlangıç noktası tipi: balloon (balon uçuşu), hotel (otel), place (ilk yer), city_center (şehir merkezi)';
COMMENT ON COLUMN places.is_time_fixed IS 'Sabit saatli etkinlik mi? (örn: balon uçuşu sabah 05:30-07:30)';
COMMENT ON COLUMN places.fixed_start_time IS 'Sabit başlangıç saati (sadece is_time_fixed=true için)';
COMMENT ON COLUMN places.fixed_end_time IS 'Sabit bitiş saati (sadece is_time_fixed=true için)';