\pset pager off

\echo '=== 1. Objetos v0.3.13 ==='

SELECT
    to_regclass(
        'mv_loss_intelligence.context_regression_policies'
    ) AS policy_table,
    to_regclass(
        'mv_loss_intelligence.context_regression_runs'
    ) AS runs_table,
    to_regclass(
        'mv_loss_intelligence.context_regression_results'
    ) AS results_table,
    to_regclass(
        'mv_reports_ucepsa_prod.li_context_regression_latest_run_v1'
    ) AS latest_run_view,
    to_regclass(
        'mv_reports_ucepsa_prod.li_context_regression_latest_results_v1'
    ) AS latest_results_view,
    to_regclass(
        'mv_reports_ucepsa_prod.li_context_regression_failures_v1'
    ) AS failures_view,
    to_regclass(
        'mv_reports_ucepsa_prod.li_context_regression_run_history_v1'
    ) AS history_view,
    to_regclass(
        'mv_reports_ucepsa_prod.li_context_regression_health_v1'
    ) AS health_view;

\echo '=== 2. Última ejecución ==='

SELECT *
FROM
    mv_reports_ucepsa_prod
    .li_context_regression_latest_run_v1;

\echo '=== 3. Salud de regresión ==='

SELECT *
FROM
    mv_reports_ucepsa_prod
    .li_context_regression_health_v1;

\echo '=== 4. Conteos de resultados coherentes ==='

SELECT COUNT(*) AS run_count_mismatches
FROM
    mv_loss_intelligence.context_regression_runs r
WHERE r.result_count <>
      (
          SELECT COUNT(*)
          FROM
              mv_loss_intelligence
              .context_regression_results x
          WHERE x.run_id =
                r.run_id
      )
   OR r.pass_count <>
      (
          SELECT COUNT(*)
          FROM
              mv_loss_intelligence
              .context_regression_results x
          WHERE x.run_id =
                r.run_id
            AND x.status = 'PASS'
      )
   OR r.warn_count <>
      (
          SELECT COUNT(*)
          FROM
              mv_loss_intelligence
              .context_regression_results x
          WHERE x.run_id =
                r.run_id
            AND x.status = 'WARN'
      )
   OR r.fail_count <>
      (
          SELECT COUNT(*)
          FROM
              mv_loss_intelligence
              .context_regression_results x
          WHERE x.run_id =
                r.run_id
            AND x.status = 'FAIL'
      )
   OR r.error_count <>
      (
          SELECT COUNT(*)
          FROM
              mv_loss_intelligence
              .context_regression_results x
          WHERE x.run_id =
                r.run_id
            AND x.status = 'ERROR'
      );

\echo '=== 5. Códigos de test únicos por ejecución ==='

SELECT COUNT(*) AS duplicate_test_codes
FROM (
    SELECT
        run_id,
        test_code,
        COUNT(*) AS n
    FROM
        mv_loss_intelligence.context_regression_results
    GROUP BY
        run_id,
        test_code
    HAVING COUNT(*) > 1
) duplicated;

\echo '=== 6. La última suite está completa ==='

SELECT
    l.run_id,
    l.overall_status,
    l.result_count,
    p.minimum_expected_test_count,
    (
        l.result_count >=
        p.minimum_expected_test_count
    ) AS suite_complete
FROM
    mv_reports_ucepsa_prod
    .li_context_regression_latest_run_v1 l
JOIN
    mv_loss_intelligence.context_regression_policies p
  ON p.tenant =
     l.tenant
 AND p.site =
     l.site
 AND p.policy_code =
     l.policy_code
WHERE p.active;

\echo '=== 7. Fallos o errores críticos en la última suite ==='

SELECT COUNT(*) AS latest_fail_or_error_count
FROM
    mv_reports_ucepsa_prod
    .li_context_regression_latest_results_v1
WHERE status IN (
    'FAIL',
    'ERROR'
);

\echo '=== 8. Contratos críticos conocidos ==='

SELECT COUNT(*) AS critical_contract_not_passed
FROM
    mv_reports_ucepsa_prod
    .li_context_regression_latest_results_v1
WHERE test_code IN (
    'CANON-001',
    'CANON-002',
    'CANON-003',
    'CANON-004',
    'WO340-001',
    'WO340-002',
    'LEGACY-001',
    'LEGACY-002',
    'CONFLICT-001',
    'CONFLICT-002',
    'CASE-001',
    'DECISION-001',
    'AGENDA-001',
    'SNAPSHOT-001',
    'SHADOW-001'
)
  AND status <> 'PASS';

\echo '=== 9. Warnings informativos ==='

SELECT
    test_code,
    test_group,
    test_title,
    status,
    severity,
    actual_json,
    detail,
    duration_ms
FROM
    mv_reports_ucepsa_prod
    .li_context_regression_failures_v1;

\echo '=== 10. Nada oficial ==='

SELECT
    (
        SELECT COUNT(*)
        FROM
            mv_loss_intelligence.context_regression_runs
        WHERE official_eligible
    ) AS official_run_rows,
    (
        SELECT COUNT(*)
        FROM
            mv_loss_intelligence.context_regression_results
        WHERE official_eligible
    ) AS official_result_rows,
    (
        SELECT COUNT(*)
        FROM
            mv_reports_ucepsa_prod
            .li_context_regression_health_v1
        WHERE official_eligible
    ) AS official_health_rows,
    (
        SELECT COUNT(*)
        FROM
            mv_reports_ucepsa_prod.li_operator_stop_queue_v2
    ) AS official_operator_queue_rows;
