"""
Шаблон для генерации SQL функции match_services.
Используйте f-строку для заполнения параметров.
"""


def generate_match_services_sql(
    table_name: str,
    function_name: str,
) -> str:
    """
    Генерирует SQL код для создания функции match_services.
    
    Args:
        table_name: Название таблицы
        function_name: Название функции
    
    Returns:
        SQL код для создания функции
    """
    sql = f"""CREATE OR REPLACE FUNCTION {function_name}(
    query_embedding vector, 
    filter jsonb DEFAULT '{{}}'::jsonb
)
RETURNS TABLE(
    id uuid, 
    content text, 
    metadata jsonb, 
    similarity double precision
)
LANGUAGE plpgsql
AS $function$
DECLARE
    filter_conditions text := '';
    filter_key text;
    filter_value jsonb;
    array_elements text;
BEGIN
    -- Проверяем, есть ли фильтры
    IF filter IS NOT NULL AND filter != '{{}}'::jsonb THEN
        FOR filter_key, filter_value IN SELECT * FROM jsonb_each(filter)
        LOOP
            -- Строковые значения
            IF jsonb_typeof(filter_value) = 'string' THEN
                filter_conditions := filter_conditions || 
                    format(' AND metadata->>%L = %L', filter_key, filter_value #>> '{{}}');
            
            -- Массивы
            ELSIF jsonb_typeof(filter_value) = 'array' THEN
                SELECT string_agg(quote_literal(elem::text), ',')
                INTO array_elements
                FROM jsonb_array_elements_text(filter_value) AS elem;
                
                IF array_elements IS NOT NULL THEN
                    filter_conditions := filter_conditions || 
                        format(' AND metadata->>%L = ANY(ARRAY[%s])', filter_key, array_elements);
                END IF;

            -- Числа и boolean
            ELSIF jsonb_typeof(filter_value) IN ('number', 'boolean') THEN
                filter_conditions := filter_conditions || 
                    format(' AND metadata->>%L = %L', filter_key, filter_value #>> '{{}}');
            END IF;
        END LOOP;
    END IF;

    -- Выполняем запрос
    RETURN QUERY EXECUTE format(
        'SELECT 
            s.id,
            s.content,
            s.metadata,
            1 - (s.embedding <=> $1) AS similarity
         FROM {table_name} s
         WHERE TRUE %s
         ORDER BY s.embedding <=> $1',
        filter_conditions
    ) USING query_embedding;
END;
$function$;
"""
    return sql