\pset pager off

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

SELECT
    to_regclass(
        'mv_loss_intelligence.context_governance_ui_consultations'
    ) AS consultations_table,
    to_regclass(
        'mv_loss_intelligence.context_governance_context_proposals'
    ) AS proposals_table,
    to_regclass(
        'mv_loss_intelligence.context_governance_context_proposal_actions'
    ) AS proposal_actions_table,
    to_regclass(
        'mv_reports_ucepsa_prod.li_context_governance_ui_queue_v2'
    ) AS queue_v2_view,
    to_regclass(
        'mv_reports_ucepsa_prod.li_context_governance_context_approval_queue_v1'
    ) AS approval_queue_view,
    to_regclass(
        'mv_reports_ucepsa_prod.li_context_governance_context_clarification_queue_v1'
    ) AS clarification_queue_view,
    to_regclass(
        'mv_reports_ucepsa_prod.li_context_governance_ui_activity_v1'
    ) AS activity_view;

\echo '=== 2. Cola v2 conserva la agenda ==='

SELECT
    (
        SELECT COUNT(*)
        FROM mv_reports_ucepsa_prod
             .li_context_governance_ui_queue_v2
    ) AS ui_v2_rows,
    (
        SELECT COUNT(*)
        FROM mv_reports_ucepsa_prod
             .li_context_governance_daily_agenda_v1
    ) AS agenda_rows;

\echo '=== 3. Workflow visible válido ==='

SELECT COUNT(*) AS invalid_workflow_status_rows
FROM mv_reports_ucepsa_prod
     .li_context_governance_ui_queue_v2
WHERE workflow_status NOT IN (
    'PENDING',
    'IN_CONSULTATION'
)
   OR review_status <> 'PENDING';

\echo '=== 4. Una consulta abierta por decisión ==='

SELECT COUNT(*) AS duplicate_open_consultations
FROM (
    SELECT
        tenant,
        site,
        decision_group_key,
        COUNT(*) AS n
    FROM mv_loss_intelligence
         .context_governance_ui_consultations
    WHERE status = 'OPEN'
    GROUP BY
        tenant,
        site,
        decision_group_key
    HAVING COUNT(*) > 1
) duplicated;

\echo '=== 5. Una propuesta activa por caso ==='

SELECT COUNT(*) AS duplicate_active_proposals
FROM (
    SELECT
        tenant,
        site,
        case_id,
        COUNT(*) AS n
    FROM mv_loss_intelligence
         .context_governance_context_proposals
    WHERE proposal_status IN (
        'PENDING',
        'NEEDS_CLARIFICATION'
    )
    GROUP BY
        tenant,
        site,
        case_id
    HAVING COUNT(*) > 1
) duplicated;

\echo '=== 6. Propuestas aprobadas tienen contexto ==='

SELECT COUNT(*) AS approved_without_context
FROM mv_loss_intelligence
     .context_governance_context_proposals
WHERE proposal_status = 'APPROVED'
  AND context_session_id IS NULL;

\echo '=== 7. Contextos aprobados conservan SHADOW ==='

SELECT COUNT(*) AS invalid_approved_contexts
FROM mv_loss_intelligence
     .context_governance_context_proposals p
JOIN mv_loss_intelligence
     .production_context_sessions s
  ON s.context_session_id = p.context_session_id
WHERE p.proposal_status = 'APPROVED'
  AND (
        s.source_ref <>
            'GOVERNANCE_PROPOSAL:'
            || p.proposal_id::text
        OR s.source_type <>
           p.proposed_source_type
        OR s.official_eligible
        OR p.official_eligible
      );

\echo '=== 8. Producción no crea contextos directamente ==='

SELECT COUNT(*) AS direct_context_creation_actions
FROM mv_loss_intelligence
     .context_governance_ui_actions
WHERE create_contexts
   OR cardinality(
          created_context_session_ids
      ) > 0;

\echo '=== 9. Idempotencia de requests ==='

SELECT
    (
        SELECT COUNT(*)
        FROM (
            SELECT
                tenant,
                site,
                request_id,
                COUNT(*) AS n
            FROM mv_loss_intelligence
                 .context_governance_ui_actions
            GROUP BY tenant, site, request_id
            HAVING COUNT(*) > 1
        ) x
    ) AS duplicate_review_requests,
    (
        SELECT COUNT(*)
        FROM (
            SELECT
                tenant,
                site,
                request_id,
                COUNT(*) AS n
            FROM mv_loss_intelligence
                 .context_governance_context_proposal_actions
            GROUP BY tenant, site, request_id
            HAVING COUNT(*) > 1
        ) x
    ) AS duplicate_proposal_action_requests;

\echo '=== 10. Colas de trabajo ==='

SELECT
    (
        SELECT COUNT(*)
        FROM mv_reports_ucepsa_prod
             .li_context_governance_context_approval_queue_v1
        WHERE proposal_status = 'PENDING'
    ) AS technical_pending_count,
    (
        SELECT COUNT(*)
        FROM mv_reports_ucepsa_prod
             .li_context_governance_context_clarification_queue_v1
    ) AS clarification_count,
    (
        SELECT COUNT(*)
        FROM mv_reports_ucepsa_prod
             .li_context_governance_ui_queue_v2
        WHERE workflow_status = 'IN_CONSULTATION'
    ) AS in_consultation_count;

\echo '=== 11. Nada oficial ==='

SELECT
    (
        SELECT COUNT(*)
        FROM mv_loss_intelligence
             .context_governance_ui_consultations
        WHERE official_eligible
    ) AS official_consultation_rows,
    (
        SELECT COUNT(*)
        FROM mv_loss_intelligence
             .context_governance_context_proposals
        WHERE official_eligible
    ) AS official_proposal_rows,
    (
        SELECT COUNT(*)
        FROM mv_loss_intelligence
             .context_governance_context_proposal_actions
        WHERE official_eligible
    ) AS official_proposal_action_rows,
    (
        SELECT COUNT(*)
        FROM mv_reports_ucepsa_prod
             .li_operator_stop_queue_v2
    ) AS official_operator_queue_rows;

\echo '=== 12. Actividad UI reciente ==='

SELECT
    activity_type,
    decision_group_key,
    proposal_id,
    actor_display_name,
    action_code,
    status,
    notes,
    created_at
FROM mv_reports_ucepsa_prod
     .li_context_governance_ui_activity_v1
ORDER BY created_at DESC
LIMIT 30;
