\pset pager off

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

SELECT
    to_regclass(
        'mv_loss_intelligence.context_decision_group_policies'
    ) AS policy_table,
    to_regclass(
        'mv_loss_intelligence.context_decision_group_reviews'
    ) AS reviews_table,
    to_regclass(
        'mv_reports_ucepsa_prod.li_context_decision_group_case_assignments_v1'
    ) AS assignments_view,
    to_regclass(
        'mv_reports_ucepsa_prod.li_context_decision_group_history_v1'
    ) AS history_view,
    to_regclass(
        'mv_reports_ucepsa_prod.li_context_decision_group_backlog_v1'
    ) AS backlog_view,
    to_regclass(
        'mv_reports_ucepsa_prod.li_context_decision_group_cases_v1'
    ) AS cases_view,
    to_regclass(
        'mv_reports_ucepsa_prod.li_canonical_context_readiness_gate_v3'
    ) AS readiness_v3_view;

\echo '=== 2. Política de consolidación ==='

SELECT
    source_case_family,
    decision_family,
    max_gap_s,
    require_same_local_day,
    review_owner,
    timezone_name,
    active
FROM
    mv_loss_intelligence
    .context_decision_group_policies
WHERE tenant = 'ucepsa'
  AND site = 'ucepsa_onpremise'
  AND policy_code =
      'CONTEXT_DECISION_GROUPS_V1'
ORDER BY source_case_family;

\echo '=== 3. Cada caso raíz aparece una sola vez ==='

SELECT COUNT(*) AS root_case_assignment_duplicates
FROM (
    SELECT
        case_id,
        COUNT(*) AS n
    FROM
        mv_reports_ucepsa_prod
        .li_context_decision_group_case_assignments_v1
    GROUP BY case_id
    HAVING COUNT(*) > 1
) duplicated;

\echo '=== 4. Casos recientes sin decisión consolidada ==='

SELECT COUNT(*) AS recent_root_cases_without_decision_group
FROM
    mv_reports_ucepsa_prod
    .li_context_review_case_history_v1 c
WHERE c.review_required
  AND (
        c.case_status = 'OPEN'
        OR c.first_seen_at >=
           now() - interval '30 days'
      )
  AND NOT EXISTS (
      SELECT 1
      FROM
          mv_reports_ucepsa_prod
          .li_context_decision_group_case_assignments_v1 a
      WHERE a.case_id =
            c.case_id
  );

\echo '=== 5. CORT-00 actividad sin contexto: casos 1/2/3/4 ==='

SELECT
    COUNT(
        DISTINCT decision_group_key
    ) AS decision_group_count_1_2_3_4,
    COUNT(*) AS linked_case_count_1_2_3_4
FROM
    mv_reports_ucepsa_prod
    .li_context_decision_group_case_assignments_v1
WHERE case_id IN (
    1,
    2,
    3,
    4
);

\echo '=== 6. CORT-00 doble arranque: casos 5/6 ==='

SELECT
    COUNT(
        DISTINCT decision_group_key
    ) AS decision_group_count_5_6,
    COUNT(*) AS linked_case_count_5_6
FROM
    mv_reports_ucepsa_prod
    .li_context_decision_group_case_assignments_v1
WHERE case_id IN (
    5,
    6
);

\echo '=== 7. CORT-02 actividad posterior: casos 7/8 ==='

SELECT
    COUNT(
        DISTINCT decision_group_key
    ) AS decision_group_count_7_8,
    COUNT(*) AS linked_case_count_7_8
FROM
    mv_reports_ucepsa_prod
    .li_context_decision_group_case_assignments_v1
WHERE case_id IN (
    7,
    8
);

\echo '=== 8. Decisiones humanas pendientes ==='

SELECT
    backlog_rank,
    decision_group_key,
    decision_status,
    max_severity,
    machine_id,
    decision_family,
    decision_title,
    root_case_ids,
    production_orders,
    odoo_workorder_ids,
    pending_root_case_count,
    reviewed_root_case_count,
    known_resolution_classifications,
    review_owner,
    review_status,
    question_text
FROM
    mv_reports_ucepsa_prod
    .li_context_decision_group_backlog_v1
ORDER BY backlog_rank
LIMIT 50;

\echo '=== 9. Comparación de carga humana ==='

SELECT
    machine_id,
    pending_review_count
        AS pending_episode_review_count,
    pending_root_case_count,
    pending_decision_group_count,
    pending_critical_decision_group_count,
    oldest_pending_decision_group_hours,
    technical_gate_pass,
    review_gate_pass,
    readiness_status,
    next_action,
    ready_for_official_ledger,
    official_ledger_eligible
FROM
    mv_reports_ucepsa_prod
    .li_canonical_context_readiness_gate_v3
ORDER BY machine_id;

\echo '=== 10. Consolidación nunca aumenta la carga humana ==='

SELECT COUNT(*) AS decision_group_count_exceeds_root_case_count
FROM
    mv_reports_ucepsa_prod
    .li_canonical_context_readiness_gate_v3
WHERE pending_decision_group_count >
      pending_root_case_count;

\echo '=== 11. READY v3 coherente ==='

SELECT COUNT(*) AS ready_v3_gate_mismatches
FROM
    mv_reports_ucepsa_prod
    .li_canonical_context_readiness_gate_v3
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 '=== 12. Nada oficial ==='

SELECT
    (
        SELECT COUNT(*)
        FROM
            mv_reports_ucepsa_prod
            .li_context_decision_group_history_v1
        WHERE official_eligible
    ) AS official_decision_group_rows,
    (
        SELECT COUNT(*)
        FROM
            mv_reports_ucepsa_prod
            .li_canonical_context_readiness_gate_v3
        WHERE ready_for_official_ledger
           OR official_ledger_eligible
    ) AS official_gate_rows,
    (
        SELECT COUNT(*)
        FROM
            mv_reports_ucepsa_prod.li_operator_stop_queue_v2
    ) AS official_operator_queue_rows;
