-- Function to backfill missing ride data from submission data
CREATE OR REPLACE FUNCTION backfill_ride_data()
RETURNS jsonb
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path = public
AS $$
DECLARE
  v_rides_updated INTEGER := 0;
  v_manufacturer_added INTEGER := 0;
  v_designer_added INTEGER := 0;
  v_ride_model_added INTEGER := 0;
  v_ride RECORD;
  v_submission RECORD;
BEGIN
  -- Find rides with missing manufacturer, designer, or ride_model that have approved submissions
  FOR v_ride IN 
    SELECT DISTINCT r.id, r.name, r.slug, r.manufacturer_id, r.designer_id, r.ride_model_id
    FROM rides r
    WHERE r.manufacturer_id IS NULL 
       OR r.designer_id IS NULL 
       OR r.ride_model_id IS NULL
  LOOP
    -- Find the most recent approved submission for this ride with the missing data
    SELECT 
      rs.manufacturer_id,
      rs.designer_id,
      rs.ride_model_id
    INTO v_submission
    FROM ride_submissions rs
    JOIN content_submissions cs ON cs.id = rs.submission_id
    WHERE rs.ride_id = v_ride.id
      AND cs.status = 'approved'
      AND (
        (v_ride.manufacturer_id IS NULL AND rs.manufacturer_id IS NOT NULL) OR
        (v_ride.designer_id IS NULL AND rs.designer_id IS NOT NULL) OR
        (v_ride.ride_model_id IS NULL AND rs.ride_model_id IS NOT NULL)
      )
    ORDER BY cs.created_at DESC
    LIMIT 1;

    -- If we found submission data, update the ride
    IF FOUND THEN
      DECLARE
        v_updated BOOLEAN := FALSE;
      BEGIN
        -- Update manufacturer if missing
        IF v_ride.manufacturer_id IS NULL AND v_submission.manufacturer_id IS NOT NULL THEN
          UPDATE rides
          SET manufacturer_id = v_submission.manufacturer_id
          WHERE id = v_ride.id;
          v_manufacturer_added := v_manufacturer_added + 1;
          v_updated := TRUE;
          RAISE NOTICE 'Added manufacturer for ride: % (id: %)', v_ride.name, v_ride.id;
        END IF;

        -- Update designer if missing
        IF v_ride.designer_id IS NULL AND v_submission.designer_id IS NOT NULL THEN
          UPDATE rides
          SET designer_id = v_submission.designer_id
          WHERE id = v_ride.id;
          v_designer_added := v_designer_added + 1;
          v_updated := TRUE;
          RAISE NOTICE 'Added designer for ride: % (id: %)', v_ride.name, v_ride.id;
        END IF;

        -- Update ride_model if missing
        IF v_ride.ride_model_id IS NULL AND v_submission.ride_model_id IS NOT NULL THEN
          UPDATE rides
          SET ride_model_id = v_submission.ride_model_id
          WHERE id = v_ride.id;
          v_ride_model_added := v_ride_model_added + 1;
          v_updated := TRUE;
          RAISE NOTICE 'Added ride model for ride: % (id: %)', v_ride.name, v_ride.id;
        END IF;

        IF v_updated THEN
          v_rides_updated := v_rides_updated + 1;
        END IF;
      END;
    END IF;
  END LOOP;

  RETURN jsonb_build_object(
    'success', true,
    'rides_updated', v_rides_updated,
    'manufacturer_added', v_manufacturer_added,
    'designer_added', v_designer_added,
    'ride_model_added', v_ride_model_added
  );
END;
$$;