\pset pager off

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

SELECT
    to_regclass(
        'mv_loss_intelligence.context_rollout_readiness_policies'
    ) AS policy_table,
    to_regclass(
        'mv_reports_ucepsa_prod.li_canonical_context_readiness_metrics_v1'
    ) AS metrics_view,
    to_regclass(
        'mv_reports_ucepsa_prod.li_canonical_context_readiness_gate_v1'
    ) AS gate_view,
    to_regclass(
        'mv_reports_ucepsa_prod.li_context_rollout_blockers_v1'
    ) AS blockers_view,
    to_regclass(
        'mv_reports_ucepsa_prod.li_context_rollout_readiness_summary_v1'
    ) AS summary_view;

\echo '=== 2. Política activa ==='

SELECT *
FROM
    mv_loss_intelligence
    .context_rollout_readiness_policies
WHERE tenant = 'ucepsa'
  AND site = 'ucepsa_onpremise'
  AND policy_code =
      'CUTTER_CONTEXT_ROLLOUT_READINESS_V1';

\echo '=== 3. Una fila por máquina ==='

SELECT
    COUNT(*) AS machine_rows,
    COUNT(
        DISTINCT machine_id
    ) AS distinct_machines,
    COUNT(*) FILTER (
        WHERE official_ledger_eligible
    ) AS official_metric_rows
FROM
    mv_reports_ucepsa_prod
    .li_canonical_context_readiness_metrics_v1;

\echo '=== 4. Rangos y duraciones ==='

SELECT
    COUNT(*) FILTER (
        WHERE observed_activity_hours < 0
    ) AS negative_observed_hours,
    COUNT(*) FILTER (
        WHERE confirmed_context_hours < 0
    ) AS negative_confirmed_hours,
    COUNT(*) FILTER (
        WHERE unconfirmed_context_hours < 0
    ) AS negative_unconfirmed_hours,
    COUNT(*) FILTER (
        WHERE conflict_context_minutes < 0
    ) AS negative_conflict_minutes,
    COUNT(*) FILTER (
        WHERE confirmed_context_pct < 0
           OR confirmed_context_pct > 100
    ) AS invalid_confirmed_pct,
    COUNT(*) FILTER (
        WHERE unconfirmed_context_pct < 0
           OR unconfirmed_context_pct > 100
    ) AS invalid_unconfirmed_pct,
    COUNT(*) FILTER (
        WHERE productive_operator_confirmed_pct < 0
           OR productive_operator_confirmed_pct > 100
    ) AS invalid_operator_pct
FROM
    mv_reports_ucepsa_prod
    .li_canonical_context_readiness_metrics_v1;

\echo '=== 5. Descomposición de actividad ==='

SELECT COUNT(*) AS activity_decomposition_errors
FROM
    mv_reports_ucepsa_prod
    .li_canonical_context_readiness_metrics_v1
WHERE confirmed_context_hours
      + unconfirmed_context_hours
      + (
          conflict_context_minutes
          / 60.0
        )
      > observed_activity_hours + 0.05;

\echo '=== 6. Gate por máquina ==='

SELECT
    machine_id,
    observed_activity_hours,
    confirmed_context_pct,
    unconfirmed_context_pct,
    recent_conflict_minutes,
    pending_review_count,
    oldest_pending_review_hours,
    critical_session_dq_count,
    sync_health_status,
    collector_health_status,
    technical_gate_pass,
    observation_gate_pass,
    review_gate_pass,
    coverage_gate_pass,
    readiness_status,
    readiness_stage,
    next_action,
    ready_for_official_ledger,
    official_ledger_eligible
FROM
    mv_reports_ucepsa_prod
    .li_canonical_context_readiness_gate_v1
ORDER BY machine_id;

\echo '=== 7. Estados READY coherentes ==='

SELECT COUNT(*) AS ready_gate_mismatches
FROM
    mv_reports_ucepsa_prod
    .li_canonical_context_readiness_gate_v1
WHERE readiness_status =
      'READY_FOR_CONTROLLED_SHADOW'
  AND NOT (
      technical_gate_pass
      AND observation_gate_pass
      AND review_gate_pass
      AND coverage_gate_pass
  );

\echo '=== 8. Bloqueos explicables ==='

SELECT *
FROM
    mv_reports_ucepsa_prod
    .li_context_rollout_blockers_v1
ORDER BY
    CASE blocker_severity
        WHEN 'CRITICAL' THEN 1
        WHEN 'WARNING' THEN 2
        ELSE 3
    END,
    machine_id,
    blocker_code;

\echo '=== 9. Resumen ==='

SELECT *
FROM
    mv_reports_ucepsa_prod
    .li_context_rollout_readiness_summary_v1;

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

SELECT
    (
        SELECT COUNT(*)
        FROM
            mv_reports_ucepsa_prod
            .li_canonical_context_readiness_gate_v1
        WHERE ready_for_official_ledger
           OR official_ledger_eligible
    ) AS official_gate_rows,
    (
        SELECT COUNT(*)
        FROM
            mv_reports_ucepsa_prod
            .li_context_rollout_blockers_v1
        WHERE official_ledger_eligible
    ) AS official_blocker_rows,
    (
        SELECT COUNT(*)
        FROM
            mv_reports_ucepsa_prod
            .li_operator_stop_queue_v2
    ) AS official_operator_queue_rows;
