
CREATE OR REPLACE FUNCTION tcg.public.FN_TCG_Statistic_Get_Many (
      a_get_all_statistic BOOLEAN
    , a_get_inactive_statistic BOOLEAN
    , a_statistic_ids TEXT
    , a_entity_type_codes TEXT
    , a_entity_record_ids TEXT
    , a_require_all_id_filters_met BOOLEAN
    , a_require_any_id_filters_met BOOLEAN
)
RETURNS TABLE (
      statistic_id INT
    , entity_type_code TEXT
    , entity_record_id TEXT
    , name TEXT
    , value_bool BOOLEAN
    , value_float DOUBLE PRECISION
    , value_interval INTERVAL
    , value_text TEXT
    , value_timestamp TIMESTAMP
    , is_bool BOOLEAN
    , is_float BOOLEAN
    , is_interval BOOLEAN
    , is_text BOOLEAN
    , is_timestamp BOOLEAN
    , display_order INT
    , active BOOLEAN
    , created_on TIMESTAMP
    , created_by_user_id INT
    , updated_last_on TIMESTAMP
    , updated_last_by_user_id INT
    , change_set_id INT
)
LANGUAGE plpgsql
AS $$
DECLARE
    v_get_all_statistic BOOLEAN;
    v_get_inactive_statistic BOOLEAN;
    v_statistic_ids TEXT;
    v_entity_type_codes TEXT;
    v_entity_record_ids TEXT;
    v_require_all_id_filters_met BOOLEAN;
    v_require_any_id_filters_met BOOLEAN;
BEGIN
    v_get_all_statistic := COALESCE(a_get_all_statistic, FALSE);
    v_get_inactive_statistic := COALESCE(a_get_inactive_statistic, FALSE);
    v_statistic_ids := TRIM(COALESCE(a_statistic_ids, ''));
    v_entity_type_codes := TRIM(COALESCE(a_entity_type_codes, ''));
    v_entity_record_ids := TRIM(COALESCE(a_entity_record_ids, ''));
    v_require_all_id_filters_met := COALESCE(a_require_all_id_filters_met, FALSE);
    v_require_any_id_filters_met := COALESCE(a_require_any_id_filters_met, FALSE);

    -- Outputs
    RETURN QUERY SELECT
          STATISTIC.statistic_id
        , STATISTIC.entity_type_code
        , STATISTIC.entity_record_id
        , STATISTIC.name
        , STATISTIC.value_bool
        , STATISTIC.value_float
        , STATISTIC.value_interval
        , STATISTIC.value_text
        , STATISTIC.value_timestamp
        , STATISTIC.is_bool
        , STATISTIC.is_float
        , STATISTIC.is_interval
        , STATISTIC.is_text
        , STATISTIC.is_timestamp
        , STATISTIC.display_order
        , STATISTIC.active
        , STATISTIC.created_on
        , STATISTIC.created_by_user_id
        , STATISTIC.updated_last_on
        , STATISTIC.updated_last_by_user_id
        , STATISTIC.change_set_id
    FROM tcg.public.TCG_Statistic STATISTIC
    WHERE
        (
            (
                    NOT v_require_all_id_filters_met
                AND NOT v_require_any_id_filters_met
            )
            OR  (
                    v_require_all_id_filters_met
                AND (v_get_all_statistic OR v_statistic_ids = '' OR STATISTIC.statistic_id = ANY(string_to_array(v_statistic_ids, ',')::INT[]))
                AND (v_get_all_statistic OR v_entity_type_codes = '' OR STATISTIC.entity_type_code = ANY(string_to_array(v_entity_type_codes, ',')))
                AND (v_get_all_statistic OR v_entity_record_ids = '' OR STATISTIC.entity_record_id = ANY(string_to_array(v_entity_record_ids, ',')))
            )
            OR  (
                    NOT v_require_all_id_filters_met
                AND v_require_any_id_filters_met
                AND (
                        v_get_all_statistic
                    OR  STATISTIC.statistic_id = ANY(string_to_array(v_statistic_ids, ',')::INT[])
                    OR  STATISTIC.entity_type_code = ANY(string_to_array(v_entity_type_codes, ','))
                    OR  STATISTIC.entity_record_id = ANY(string_to_array(v_entity_record_ids, ','))
                )
            )
        )
        AND (
                v_get_inactive_statistic
            OR  STATISTIC.active
        )
    ORDER BY
          STATISTIC.entity_type_code
        , STATISTIC.entity_record_id
        , STATISTIC.display_order
        , STATISTIC.statistic_id
    ;
END;
$$;


SELECT *
FROM tcg.public.FN_TCG_Statistic_Get_Many (
      a_get_all_statistic := FALSE
    , a_get_inactive_statistic := FALSE
    , a_statistic_ids := ''
    , a_entity_type_codes := 'deck'
    , a_entity_record_ids := '1,2'
    , a_require_all_id_filters_met := TRUE
    , a_require_any_id_filters_met := TRUE
)
;


SELECT *
FROM tcg.public.TCG_Statistic
;

SELECT COUNT(*)
FROM tcg.public.TCG_Statistic S
WHERE S.entity_type_code = 'deck'
;

SELECT COUNT(*)
FROM tcg.public.TCG_Statistic S
WHERE S.entity_type_code = 'user'
;

SELECT COUNT(*)
FROM tcg.public.TCG_Statistic S
WHERE S.entity_type_code = 'user_deck_link'
;
