\pset pager off

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

SELECT
    to_regclass(
        'mv_loss_intelligence.context_review_case_policies'
    ) AS policy_table,
    to_regclass(
        'mv_loss_intelligence.context_review_cases'
    ) AS cases_table,
    to_regclass(
        'mv_loss_intelligence.context_review_case_evidence'
    ) AS evidence_table,
    to_regclass(
        'mv_loss_intelligence.context_review_case_reviews'
    ) AS reviews_table,
    to_regclass(
        'mv_loss_intelligence.context_review_case_builder_state'
    ) AS state_table,
    to_regclass(
        'mv_reports_ucepsa_prod.li_context_review_case_history_v1'
    ) AS history_view,
    to_regclass(
        'mv_reports_ucepsa_prod.li_context_review_case_backlog_v1'
    ) AS backlog_view,
    to_regclass(
        'mv_reports_ucepsa_prod.li_context_review_case_evidence_v1'
    ) AS evidence_view,
    to_regclass(
        'mv_reports_ucepsa_prod.li_context_review_case_builder_health_v1'
    ) AS health_view,
    to_regclass(
        'mv_reports_ucepsa_prod.li_canonical_context_readiness_gate_v2'
    ) AS readiness_v2_view;

\echo '=== 2. Salud del constructor ==='

SELECT *
FROM
    mv_reports_ucepsa_prod
    .li_context_review_case_builder_health_v1;

\echo '=== 3. Evidencias no pertenecen a varios casos ==='

SELECT COUNT(*) AS evidence_link_duplicates
FROM (
    SELECT
        episode_id,
        COUNT(*) AS n
    FROM
        mv_loss_intelligence.context_review_case_evidence
    GROUP BY episode_id
    HAVING COUNT(*) > 1
) duplicated;

\echo '=== 4. Episodios recientes con revisión no quedan sin caso ==='

SELECT COUNT(*) AS review_episodes_without_case
FROM
    mv_reports_ucepsa_prod
    .li_shopfloor_context_incident_history_v1 h
WHERE h.review_required
  AND (
        h.episode_status = 'OPEN'
        OR h.first_seen_at >=
           now() - interval '30 days'
      )
  AND NOT EXISTS (
      SELECT 1
      FROM
          mv_loss_intelligence.context_review_case_evidence e
      WHERE e.episode_id =
            h.episode_id
  );

\echo '=== 5. Conflicto 7/8/9 agrupado en un caso raíz ==='

SELECT
    COUNT(
        DISTINCT case_id
    ) AS root_case_count_7_8_9,
    COUNT(*) AS evidence_count_7_8_9
FROM
    mv_loss_intelligence.context_review_case_evidence
WHERE episode_id IN (
    7,
    8,
    9
);

\echo '=== 6. Detalle del caso raíz 7/8/9 ==='

SELECT DISTINCT
    c.case_id,
    c.case_family,
    c.machine_id,
    c.case_title,
    c.question_text,
    c.case_status,
    c.max_severity,
    c.linked_evidence_count,
    c.episode_ids,
    c.production_orders,
    c.odoo_workorder_ids,
    c.review_owner,
    c.review_status,
    c.official_eligible
FROM
    mv_reports_ucepsa_prod
    .li_context_review_case_history_v1 c
JOIN
    mv_loss_intelligence.context_review_case_evidence e
  ON e.case_id =
     c.case_id
WHERE e.episode_id IN (
    7,
    8,
    9
);

\echo '=== 7. Backlog humano por caso raíz ==='

SELECT
    backlog_rank,
    case_id,
    case_status,
    max_severity,
    machine_id,
    case_title,
    linked_evidence_count,
    production_orders,
    odoo_workorder_ids,
    review_owner,
    review_status,
    question_text
FROM
    mv_reports_ucepsa_prod
    .li_context_review_case_backlog_v1
ORDER BY backlog_rank
LIMIT 50;

\echo '=== 8. Readiness v2 ==='

SELECT
    machine_id,
    pending_review_count
        AS pending_episode_review_count,
    pending_root_case_count,
    pending_critical_root_case_count,
    oldest_pending_root_case_hours,
    builder_health_status,
    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_v2
ORDER BY machine_id;

\echo '=== 9. READY v2 coherente ==='

SELECT COUNT(*) AS ready_v2_gate_mismatches
FROM
    mv_reports_ucepsa_prod
    .li_canonical_context_readiness_gate_v2
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 '=== 10. Nada oficial ==='

SELECT
    (
        SELECT COUNT(*)
        FROM
            mv_loss_intelligence.context_review_cases
        WHERE official_eligible
    ) AS official_case_rows,
    (
        SELECT COUNT(*)
        FROM
            mv_reports_ucepsa_prod
            .li_canonical_context_readiness_gate_v2
        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;
