-- Aggregate-only audit of source-specific inspection result encodings.
--
-- Values are controlled inspection categories, never identifiers. Counts below
-- 100 are suppressed so accidental free-text anomalies cannot be surfaced.
BEGIN TRANSACTION READ ONLY;

WITH normalized AS MATERIALIZED (
    SELECT
        lower(coalesce(nullif(btrim(county), ''), '<blank/null>')) AS source_era,
        upper(coalesce(nullif(btrim(overall_result), ''), '<BLANK/NULL>'))
            AS overall_result,
        upper(coalesce(nullif(btrim(obd_result), ''), '<BLANK/NULL>'))
            AS obd_result,
        lower(coalesce(nullif(btrim(program_type), ''), '<blank/null>'))
            AS program_type,
        upper(coalesce(nullif(btrim(test_type), ''), '<BLANK/NULL>'))
            AS test_type
    FROM data.inspection_search
    WHERE test_start >= timestamp '2010-01-01'
),
overall_counts AS (
    SELECT
        'source_overall_result'::text AS audit_section,
        source_era,
        overall_result AS value_1,
        CASE
            WHEN overall_result IN ('PASS', 'P') THEN 'pass'
            WHEN overall_result IN ('FAIL', 'F') THEN 'fail'
            WHEN overall_result = 'REJECT' THEN 'reject'
            WHEN overall_result = 'ABORT' THEN 'abort'
            ELSE '<unrecognized>'
        END::text AS value_2,
        NULL::text AS value_3,
        count(*) AS records
    FROM normalized
    GROUP BY 1, 2, 3, 4
    HAVING count(*) >= 100
),
utah_unrecognized_context AS (
    SELECT
        'utah_unrecognized_context'::text AS audit_section,
        source_era,
        overall_result AS value_1,
        obd_result AS value_2,
        program_type || ' / ' || test_type AS value_3,
        count(*) AS records
    FROM normalized
    WHERE source_era = 'utah'
      AND overall_result NOT IN ('PASS', 'P', 'FAIL', 'F', 'REJECT', 'ABORT')
    GROUP BY 1, 2, 3, 4, 5
    HAVING count(*) >= 100
),
overall_obd_crosscheck AS (
    SELECT
        'recognized_overall_vs_obd'::text AS audit_section,
        source_era,
        overall_result AS value_1,
        obd_result AS value_2,
        program_type || ' / ' || test_type AS value_3,
        count(*) AS records
    FROM normalized
    WHERE overall_result IN ('PASS', 'P', 'FAIL', 'F', 'REJECT', 'ABORT')
      AND obd_result IN ('PASS', 'P', 'FAIL', 'F', 'REJECT', 'ABORT')
    GROUP BY 1, 2, 3, 4, 5
    HAVING count(*) >= 100
),
binary_crosscheck AS (
    SELECT
        'binary_overall_vs_obd'::text AS audit_section,
        source_era,
        CASE
            WHEN (overall_result IN ('PASS', 'P'))
                 = (obd_result IN ('PASS', 'P'))
            THEN 'agree'
            ELSE 'disagree'
        END::text AS value_1,
        NULL::text AS value_2,
        NULL::text AS value_3,
        count(*) AS records
    FROM normalized
    WHERE overall_result IN ('PASS', 'P', 'FAIL', 'F', 'REJECT', 'ABORT')
      AND obd_result IN ('PASS', 'P', 'FAIL', 'F', 'REJECT', 'ABORT')
    GROUP BY 1, 2, 3
    HAVING count(*) >= 100
),
ranked AS (
    SELECT
        *,
        row_number() OVER (
            PARTITION BY audit_section, source_era
            ORDER BY records DESC, value_1, value_2, value_3
        ) AS frequency_rank
    FROM (
        SELECT * FROM overall_counts
        UNION ALL
        SELECT * FROM utah_unrecognized_context
        UNION ALL
        SELECT * FROM overall_obd_crosscheck
        UNION ALL
        SELECT * FROM binary_crosscheck
    )
)
SELECT
    audit_section,
    source_era,
    value_1,
    value_2,
    value_3,
    records
FROM ranked
WHERE frequency_rank <= 50
ORDER BY audit_section, source_era, records DESC;

ROLLBACK;
