DROP PROCEDURE IF EXISTS mocha_search_count;

CREATE PROCEDURE mocha_search_count
(
    IN p_query VARCHAR(50)
)
BEGIN
    DECLARE p_tenant_id INT;
    SET p_tenant_id = mocha_get_current_tenant();

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

END;
