\pset pager off

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

SELECT
    to_regclass(
        'mv_loss_intelligence.context_governance_daily_policies'
    ) AS policy_table,
    to_regclass(
        'mv_loss_intelligence.context_governance_agenda_snapshots'
    ) AS snapshots_table,
    to_regclass(
        'mv_loss_intelligence.context_governance_agenda_snapshot_items'
    ) AS snapshot_items_table,
    to_regclass(
        'mv_reports_ucepsa_prod.li_context_governance_decision_evidence_pack_v1'
    ) AS evidence_pack_view,
    to_regclass(
        'mv_reports_ucepsa_prod.li_context_governance_daily_agenda_v1'
    ) AS agenda_view,
    to_regclass(
        'mv_reports_ucepsa_prod.li_context_governance_daily_summary_v1'
    ) AS summary_view,
    to_regclass(
        'mv_reports_ucepsa_prod.li_context_governance_agenda_snapshot_history_v1'
    ) AS snapshot_history_view;

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

SELECT
    timezone_name,
    attention_after_hours,
    overdue_after_hours,
    agenda_limit,
    active
FROM
    mv_loss_intelligence
    .context_governance_daily_policies
WHERE tenant = 'ucepsa'
  AND site = 'ucepsa_onpremise'
  AND policy_code =
      'CONTEXT_DAILY_GOVERNANCE_V1';

\echo '=== 3. Una fila de agenda por decisión pendiente ==='

SELECT
    (
        SELECT COUNT(*)
        FROM
            mv_reports_ucepsa_prod
            .li_context_governance_daily_agenda_v1
    ) AS agenda_rows,
    (
        SELECT COUNT(*)
        FROM
            mv_reports_ucepsa_prod
            .li_context_decision_group_backlog_v1
    ) AS decision_backlog_rows;

\echo '=== 4. Decisiones duplicadas en agenda ==='

SELECT COUNT(*) AS duplicated_agenda_decisions
FROM (
    SELECT
        decision_group_key,
        COUNT(*) AS n
    FROM
        mv_reports_ucepsa_prod
        .li_context_governance_daily_agenda_v1
    GROUP BY decision_group_key
    HAVING COUNT(*) > 1
) duplicated;

\echo '=== 5. Ranking continuo ==='

WITH ranked AS (
    SELECT
        agenda_rank,
        ROW_NUMBER() OVER (
            ORDER BY agenda_rank
        ) AS expected_rank
    FROM
        mv_reports_ucepsa_prod
        .li_context_governance_daily_agenda_v1
)
SELECT COUNT(*) AS agenda_rank_mismatches
FROM ranked
WHERE agenda_rank <>
      expected_rank;

\echo '=== 6. Evidencia de las tres decisiones actuales ==='

SELECT
    decision_group_key,
    machine_id,
    linked_root_case_count,
    linked_episode_count,
    root_case_ids,
    episode_ids,
    production_orders,
    odoo_workorder_ids,
    operator_names,
    review_owner,
    review_status
FROM
    mv_reports_ucepsa_prod
    .li_context_governance_decision_evidence_pack_v1
WHERE decision_group_key IN (
    'DECISION:UNCONFIRMED_ACTIVITY:CORT-00:20260720:1',
    'DECISION:ODOO_ORDER_START_CONFLICT:CORT-00:20260720:5',
    'DECISION:UNCONFIRMED_ACTIVITY:CORT-02:20260720:7'
)
ORDER BY decision_group_key;

\echo '=== 7. Resumen diario ==='

SELECT *
FROM
    mv_reports_ucepsa_prod
    .li_context_governance_daily_summary_v1;

\echo '=== 8. Tramos de atención válidos ==='

SELECT COUNT(*) AS invalid_attention_status_rows
FROM
    mv_reports_ucepsa_prod
    .li_context_governance_daily_agenda_v1
WHERE attention_status NOT IN (
    'RECENT',
    'ATTENTION',
    'OVERDUE'
)
   OR pending_age_hours < 0
   OR review_due_at <
      first_seen_at;

\echo '=== 9. Snapshots coherentes ==='

SELECT COUNT(*) AS snapshot_item_count_mismatches
FROM
    mv_loss_intelligence
    .context_governance_agenda_snapshots s
WHERE s.item_count <>
      (
          SELECT COUNT(*)
          FROM
              mv_loss_intelligence
              .context_governance_agenda_snapshot_items i
          WHERE i.snapshot_id =
                s.snapshot_id
      );

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

SELECT
    (
        SELECT COUNT(*)
        FROM
            mv_loss_intelligence
            .context_governance_agenda_snapshots
        WHERE official_eligible
    ) AS official_snapshot_rows,
    (
        SELECT COUNT(*)
        FROM
            mv_loss_intelligence
            .context_governance_agenda_snapshot_items
        WHERE official_eligible
    ) AS official_snapshot_item_rows,
    (
        SELECT COUNT(*)
        FROM
            mv_reports_ucepsa_prod
            .li_context_governance_daily_summary_v1
        WHERE official_eligible
    ) AS official_summary_rows,
    (
        SELECT COUNT(*)
        FROM
            mv_reports_ucepsa_prod.li_operator_stop_queue_v2
    ) AS official_operator_queue_rows;
