-- Fix create_submission_with_items to include temp_location_data for park submissions
CREATE OR REPLACE FUNCTION public.create_submission_with_items(
  p_submission_id UUID,
  p_entity_type TEXT,
  p_action_type TEXT,
  p_items JSONB,
  p_user_id UUID
) RETURNS UUID AS $$
DECLARE
  v_item JSONB;
  v_item_type TEXT;
  v_item_data JSONB;
  v_depends_on INTEGER;
  v_order_index INTEGER;
  v_created_ids UUID[] := ARRAY[]::UUID[];
  v_submission_item_id UUID;
  v_entity_submission_id UUID;
BEGIN
  -- Loop through items array
  FOR v_item IN SELECT * FROM jsonb_array_elements(p_items)
  LOOP
    v_item_type := v_item->>'item_type';
    v_item_data := v_item->'item_data';
    v_depends_on := (v_item->>'depends_on')::INTEGER;
    v_order_index := (v_item->>'order_index')::INTEGER;

    -- Resolve dependency references
    IF v_depends_on IS NOT NULL THEN
      v_item_data := v_item_data || jsonb_build_object(
        v_item->>'dependency_field',
        v_created_ids[v_depends_on + 1]
      );
    END IF;

    -- Create submission based on entity type
    IF v_item_type = 'park' THEN
      INSERT INTO park_submissions (
        submission_id, name, slug, description, park_type, status,
        opening_date, opening_date_precision, closing_date, closing_date_precision,
        location_id, temp_location_data, operator_id, property_owner_id,
        website_url, phone, email,
        banner_image_url, banner_image_id, card_image_url, card_image_id
      ) VALUES (
        p_submission_id,
        v_item_data->>'name',
        v_item_data->>'slug',
        v_item_data->>'description',
        v_item_data->>'park_type',
        v_item_data->>'status',
        (v_item_data->>'opening_date')::DATE,
        v_item_data->>'opening_date_precision',
        (v_item_data->>'closing_date')::DATE,
        v_item_data->>'closing_date_precision',
        (v_item_data->>'location_id')::UUID,
        v_item_data->'temp_location_data',
        (v_item_data->>'operator_id')::UUID,
        (v_item_data->>'property_owner_id')::UUID,
        v_item_data->>'website_url',
        v_item_data->>'phone',
        v_item_data->>'email',
        v_item_data->>'banner_image_url',
        v_item_data->>'banner_image_id',
        v_item_data->>'card_image_url',
        v_item_data->>'card_image_id'
      ) RETURNING id INTO v_entity_submission_id;

    ELSIF v_item_type = 'ride' THEN
      INSERT INTO ride_submissions (
        submission_id, name, slug, description, category, status,
        opening_date, opening_date_precision, closing_date, closing_date_precision,
        park_id, manufacturer_id, designer_id, ride_model_id,
        banner_image_url, banner_image_id, card_image_url, card_image_id
      ) VALUES (
        p_submission_id,
        v_item_data->>'name',
        v_item_data->>'slug',
        v_item_data->>'description',
        v_item_data->>'category',
        v_item_data->>'status',
        (v_item_data->>'opening_date')::DATE,
        v_item_data->>'opening_date_precision',
        (v_item_data->>'closing_date')::DATE,
        v_item_data->>'closing_date_precision',
        (v_item_data->>'park_id')::UUID,
        (v_item_data->>'manufacturer_id')::UUID,
        (v_item_data->>'designer_id')::UUID,
        (v_item_data->>'ride_model_id')::UUID,
        v_item_data->>'banner_image_url',
        v_item_data->>'banner_image_id',
        v_item_data->>'card_image_url',
        v_item_data->>'card_image_id'
      ) RETURNING id INTO v_entity_submission_id;

    ELSIF v_item_type IN ('manufacturer', 'operator', 'designer', 'property_owner') THEN
      INSERT INTO company_submissions (
        submission_id, name, slug, description, company_type,
        founded_year, headquarters_location, website_url,
        banner_image_url, banner_image_id, card_image_url, card_image_id
      ) VALUES (
        p_submission_id,
        v_item_data->>'name',
        v_item_data->>'slug',
        v_item_data->>'description',
        v_item_type,
        (v_item_data->>'founded_year')::INTEGER,
        v_item_data->>'headquarters_location',
        v_item_data->>'website_url',
        v_item_data->>'banner_image_url',
        v_item_data->>'banner_image_id',
        v_item_data->>'card_image_url',
        v_item_data->>'card_image_id'
      ) RETURNING id INTO v_entity_submission_id;

    ELSIF v_item_type = 'ride_model' THEN
      INSERT INTO ride_model_submissions (
        submission_id, name, slug, description, manufacturer_id, category,
        banner_image_url, banner_image_id, card_image_url, card_image_id
      ) VALUES (
        p_submission_id,
        v_item_data->>'name',
        v_item_data->>'slug',
        v_item_data->>'description',
        (v_item_data->>'manufacturer_id')::UUID,
        v_item_data->>'category',
        v_item_data->>'banner_image_url',
        v_item_data->>'banner_image_id',
        v_item_data->>'card_image_url',
        v_item_data->>'card_image_id'
      ) RETURNING id INTO v_entity_submission_id;

    ELSE
      RAISE EXCEPTION 'Unsupported item type: %', v_item_type;
    END IF;

    -- Create submission_item record linking to the entity submission
    INSERT INTO submission_items (
      submission_id,
      item_type,
      action_type,
      order_index,
      depends_on,
      park_submission_id,
      ride_submission_id,
      company_submission_id,
      ride_model_submission_id
    ) VALUES (
      p_submission_id,
      v_item_type,
      p_action_type,
      v_order_index,
      CASE WHEN v_depends_on IS NOT NULL THEN v_created_ids[v_depends_on + 1] ELSE NULL END,
      CASE WHEN v_item_type = 'park' THEN v_entity_submission_id ELSE NULL END,
      CASE WHEN v_item_type = 'ride' THEN v_entity_submission_id ELSE NULL END,
      CASE WHEN v_item_type IN ('manufacturer', 'operator', 'designer', 'property_owner') THEN v_entity_submission_id ELSE NULL END,
      CASE WHEN v_item_type = 'ride_model' THEN v_entity_submission_id ELSE NULL END
    ) RETURNING id INTO v_submission_item_id;

    -- Track created submission item IDs in order for dependency resolution
    v_created_ids := array_append(v_created_ids, v_submission_item_id);
  END LOOP;

  RETURN p_submission_id;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER SET search_path = public;