-- ============================================================================
-- Fix Timeline Event Updates and Deletes
-- Adds support for timeline_event and milestone entity types
-- ============================================================================

-- Update function to support timeline event updates
CREATE OR REPLACE FUNCTION update_entity_from_submission(
  p_entity_type TEXT,
  p_data JSONB,
  p_entity_id UUID,
  p_updated_by UUID
)
RETURNS UUID
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path = 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),
        ride_type = COALESCE(p_data->>'ride_type', ride_type),
        status = COALESCE(p_data->>'status', status),
        manufacturer_id = COALESCE((p_data->>'manufacturer_id')::UUID, manufacturer_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 'manufacturer', 'operator', 'property_owner', 'designer' THEN
      UPDATE companies SET
        name = COALESCE(p_data->>'name', name),
        slug = COALESCE(p_data->>'slug', slug),
        description = COALESCE(p_data->>'description', description),
        website_url = COALESCE(p_data->>'website_url', website_url),
        founded_year = COALESCE((p_data->>'founded_year')::INTEGER, founded_year),
        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),
        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;
      
    WHEN 'timeline_event', 'milestone' THEN
      UPDATE entity_timeline_events SET
        event_type = COALESCE(p_data->>'event_type', event_type),
        event_date = COALESCE((p_data->>'event_date')::DATE, event_date),
        event_date_precision = COALESCE(p_data->>'event_date_precision', event_date_precision),
        title = COALESCE(p_data->>'title', title),
        description = COALESCE(p_data->>'description', description),
        from_value = COALESCE(p_data->>'from_value', from_value),
        to_value = COALESCE(p_data->>'to_value', to_value),
        from_entity_id = COALESCE((p_data->>'from_entity_id')::UUID, from_entity_id),
        to_entity_id = COALESCE((p_data->>'to_entity_id')::UUID, to_entity_id),
        from_location_id = COALESCE((p_data->>'from_location_id')::UUID, from_location_id),
        to_location_id = COALESCE((p_data->>'to_location_id')::UUID, to_location_id),
        updated_at = NOW()
      WHERE id = p_entity_id;
      
    ELSE
      RAISE EXCEPTION 'Unsupported entity type for update: %', p_entity_type
        USING ERRCODE = '22023';
  END CASE;
  
  RETURN p_entity_id;
END;
$$;

-- Update function to support timeline event deletion
CREATE OR REPLACE FUNCTION delete_entity_from_submission(
  p_entity_type TEXT,
  p_entity_id UUID,
  p_deleted_by UUID
)
RETURNS VOID
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path = public
AS $$
BEGIN
  CASE p_entity_type
    WHEN 'park' THEN
      DELETE FROM parks WHERE id = p_entity_id;
    WHEN 'ride' THEN
      DELETE FROM rides WHERE id = p_entity_id;
    WHEN 'manufacturer', 'operator', 'property_owner', 'designer' THEN
      DELETE FROM companies WHERE id = p_entity_id;
    WHEN 'ride_model' THEN
      DELETE FROM ride_models WHERE id = p_entity_id;
    WHEN 'timeline_event', 'milestone' THEN
      DELETE FROM entity_timeline_events WHERE id = p_entity_id;
    ELSE
      RAISE EXCEPTION 'Unsupported entity type for deletion: %', p_entity_type
        USING ERRCODE = '22023';
  END CASE;
END;
$$;