\pset pager off
\echo '=== 1. Política telemetry-aware ==='
SELECT policy_code, require_telemetry_coverage, telemetry_gap_s, telemetry_tail_s,
       noise_below_s, operator_individual_from_s
FROM mv_loss_intelligence.loss_policies
WHERE tenant = 'ucepsa' AND site = 'ucepsa_onpremise' AND policy_code = 'CUTTER_STOPS_V1';

\echo '=== 2. Estado operativo power-aware ==='
SELECT machine_id, operational_state, machine_state_label, telemetry_fresh,
       round(age_s::numeric, 1) AS age_s, machine_running,
       order_present, order_coherent, production_order
FROM mv_reports_ucepsa_prod.machine_operational_state_v1
ORDER BY machine_id;

\echo '=== 3. Islas de telemetría más recientes ==='
SELECT machine_id, coverage_from, coverage_to, sample_count, sampled_span_s
FROM mv_reports_ucepsa_prod.li_telemetry_coverage_v1
ORDER BY coverage_to DESC
LIMIT 15;

\echo '=== 4. Invariantes temporales: debe devolver 0 ==='
SELECT COUNT(*) AS invalid_slice_rows
FROM mv_reports_ucepsa_prod.li_stop_eligible_slices_v2
WHERE eligible_from < order_overlap_from
   OR eligible_to > order_overlap_to
   OR eligible_from < telemetry_coverage_from
   OR eligible_to > telemetry_coverage_to
   OR eligible_to <= eligible_from
   OR eligible_duration_s <= 0;

\echo '=== 5. Duración elegible nunca supera la raw: debe devolver 0 ==='
SELECT COUNT(*) AS duration_overflow_rows
FROM mv_reports_ucepsa_prod.li_stop_candidates_v2
WHERE eligible_duration_s > raw_duration_s + 0.01
   OR eligible_duration_s > order_overlap_duration_s + 0.01;

\echo '=== 6. Comparación v0.1 frente a v0.2 ==='
SELECT * FROM mv_reports_ucepsa_prod.li_eligibility_comparison_v1;

\echo '=== 7. Resumen v0.2 ==='
SELECT *
FROM mv_reports_ucepsa_prod.li_shadow_summary_v2
ORDER BY machine_id, eligibility_status;

\echo '=== 8. Primeros candidatos power-aware ==='
SELECT shadow_rank, machine_id, order_ref, product_name, event_count,
       started_at, ended_at, duration_min, capacity_loss_eur,
       order_clipped, telemetry_clipped, crosses_order_boundary, queue_status
FROM mv_reports_ucepsa_prod.li_shadow_review_queue_v2
ORDER BY shadow_rank
LIMIT 25;

\echo '=== 9. Diagnóstico de exclusiones ==='
SELECT diagnostic_status, machine_id, COUNT(*) AS stop_count,
       ROUND(SUM(raw_duration_min)::numeric, 2) AS raw_minutes
FROM mv_reports_ucepsa_prod.li_stop_exclusion_diagnostics_v2
GROUP BY diagnostic_status, machine_id
ORDER BY machine_id, diagnostic_status;

\echo '=== 10. Cola oficial v2: debe estar vacía en SHADOW ==='
SELECT COUNT(*) AS official_operator_queue_v2_rows
FROM mv_reports_ucepsa_prod.li_operator_stop_queue_v2;

\echo '=== 11. Calidad de dato v2 ==='
SELECT * FROM mv_reports_ucepsa_prod.li_data_quality_v2
ORDER BY check_code;

\echo '=== 12. Candidatos v2 de más de 12 h para revisión manual ==='
SELECT shadow_rank, machine_id, order_ref, started_at, ended_at,
       duration_min, telemetry_clipped, order_clipped
FROM mv_reports_ucepsa_prod.li_shadow_review_queue_v2
WHERE duration_min >= 720
ORDER BY duration_min DESC;
