-- Fix update_entity_from_submission function with correct category fields
-- This migration corrects two critical bugs:
-- 1. Removes non-existent 'ride_type' column reference for rides
-- 2. Adds missing 'category' field for both rides and ride_models

DROP FUNCTION IF EXISTS update_entity_from_submission(TEXT, JSONB, UUID, UUID);

CREATE OR REPLACE FUNCTION update_entity_from_submission(
  p_entity_type TEXT,
  p_data JSONB,
  p_entity_id UUID,
  p_changed_by UUID
) RETURNS UUID
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path TO 'public'
AS $$
BEGIN
  CASE p_entity_type
    WHEN 'park' THEN
      UPDATE parks SET
        name = COALESCE(p_data->>'name', name),
        slug = COALESCE(p_data->>'slug', slug),
        description = COALESCE(p_data->>'description', description),
        park_type = COALESCE(p_data->>'park_type', park_type),
        status = COALESCE(p_data->>'status', status),
        location_id = COALESCE((p_data->>'location_id')::UUID, location_id),
        operator_id = COALESCE((p_data->>'operator_id')::UUID, operator_id),
        property_owner_id = COALESCE((p_data->>'property_owner_id')::UUID, property_owner_id),
        opening_date = COALESCE((p_data->>'opening_date')::DATE, opening_date),
        closing_date = COALESCE((p_data->>'closing_date')::DATE, closing_date),
        opening_date_precision = COALESCE(p_data->>'opening_date_precision', opening_date_precision),
        closing_date_precision = COALESCE(p_data->>'closing_date_precision', closing_date_precision),
        website_url = COALESCE(p_data->>'website_url', website_url),
        phone = COALESCE(p_data->>'phone', phone),
        email = COALESCE(p_data->>'email', email),
        banner_image_url = COALESCE(p_data->>'banner_image_url', banner_image_url),
        banner_image_id = COALESCE(p_data->>'banner_image_id', banner_image_id),
        card_image_url = COALESCE(p_data->>'card_image_url', card_image_url),
        card_image_id = COALESCE(p_data->>'card_image_id', card_image_id),
        updated_at = NOW()
      WHERE id = p_entity_id;

    WHEN 'ride' THEN
      UPDATE rides SET
        name = COALESCE(p_data->>'name', name),
        slug = COALESCE(p_data->>'slug', slug),
        park_id = COALESCE((p_data->>'park_id')::UUID, park_id),
        category = COALESCE(p_data->>'category', category),
        status = COALESCE(p_data->>'status', status),
        manufacturer_id = COALESCE((p_data->>'manufacturer_id')::UUID, manufacturer_id),
        designer_id = COALESCE((p_data->>'designer_id')::UUID, designer_id),
        ride_model_id = COALESCE((p_data->>'ride_model_id')::UUID, ride_model_id),
        opening_date = COALESCE((p_data->>'opening_date')::DATE, opening_date),
        closing_date = COALESCE((p_data->>'closing_date')::DATE, closing_date),
        opening_date_precision = COALESCE(p_data->>'opening_date_precision', opening_date_precision),
        closing_date_precision = COALESCE(p_data->>'closing_date_precision', closing_date_precision),
        description = COALESCE(p_data->>'description', description),
        banner_image_url = COALESCE(p_data->>'banner_image_url', banner_image_url),
        banner_image_id = COALESCE(p_data->>'banner_image_id', banner_image_id),
        card_image_url = COALESCE(p_data->>'card_image_url', card_image_url),
        card_image_id = COALESCE(p_data->>'card_image_id', card_image_id),
        updated_at = NOW()
      WHERE id = p_entity_id;

    WHEN 'company' THEN
      UPDATE companies SET
        name = COALESCE(p_data->>'name', name),
        slug = COALESCE(p_data->>'slug', slug),
        description = COALESCE(p_data->>'description', description),
        company_type = COALESCE(p_data->>'company_type', company_type),
        person_type = COALESCE(p_data->>'person_type', person_type),
        founded_year = COALESCE((p_data->>'founded_year')::INTEGER, founded_year),
        founded_date = COALESCE((p_data->>'founded_date')::DATE, founded_date),
        founded_date_precision = COALESCE(p_data->>'founded_date_precision', founded_date_precision),
        headquarters_location = COALESCE(p_data->>'headquarters_location', headquarters_location),
        website_url = COALESCE(p_data->>'website_url', website_url),
        logo_url = COALESCE(p_data->>'logo_url', logo_url),
        banner_image_url = COALESCE(p_data->>'banner_image_url', banner_image_url),
        banner_image_id = COALESCE(p_data->>'banner_image_id', banner_image_id),
        card_image_url = COALESCE(p_data->>'card_image_url', card_image_url),
        card_image_id = COALESCE(p_data->>'card_image_id', card_image_id),
        updated_at = NOW()
      WHERE id = p_entity_id;

    WHEN 'ride_model' THEN
      UPDATE ride_models SET
        name = COALESCE(p_data->>'name', name),
        slug = COALESCE(p_data->>'slug', slug),
        manufacturer_id = COALESCE((p_data->>'manufacturer_id')::UUID, manufacturer_id),
        category = COALESCE(p_data->>'category', category),
        ride_type = COALESCE(p_data->>'ride_type', ride_type),
        description = COALESCE(p_data->>'description', description),
        banner_image_url = COALESCE(p_data->>'banner_image_url', banner_image_url),
        banner_image_id = COALESCE(p_data->>'banner_image_id', banner_image_id),
        card_image_url = COALESCE(p_data->>'card_image_url', card_image_url),
        card_image_id = COALESCE(p_data->>'card_image_id', card_image_id),
        updated_at = NOW()
      WHERE id = p_entity_id;

    ELSE
      RAISE EXCEPTION 'Unsupported entity type: %', p_entity_type;
  END CASE;

  RETURN p_entity_id;
END;
$$;

GRANT EXECUTE ON FUNCTION update_entity_from_submission TO authenticated;