DROP PROCEDURE IF EXISTS mocha_search_query;

CREATE PROCEDURE mocha_search_query
(
    IN p_query VARCHAR(50)
)
BEGIN
    DECLARE p_tenant_id INT;
    DECLARE z_effective_date DATETIME;

    SET p_tenant_id = mocha_get_current_tenant();

    SELECT * FROM mocha_attributes
    WHERE tenant_id = p_tenant_id
    AND LOWER(att_value) LIKE CONCAT('%', LOWER(p_query), '%')
    AND att_effective_date <= z_effective_date
    ORDER BY att_effective_date;

END;
