-- =============================================
-- MANUTENCAO / RESET CONTROLADO - SN <-> GLPI
-- =============================================
-- Use este arquivo apenas para manutencao operacional.
-- Nao execute em producao sem backup e janela aprovada.

BEGIN;

-- Diagnostico rapido
SELECT * FROM ticket_sync WHERE glpi_sync_status != 'ignored';
SELECT * FROM tickets_sn WHERE status != 'Encerrado' AND status != 'Encerrado - Omitido';
SELECT * FROM public.location_mapping;

-- Limpeza de tickets abertos (mantem chamados encerrados)
DELETE FROM tickets_sn
WHERE status != 'Encerrado'
  AND status != 'Encerrado - Omitido';

-- Reinicia watermarks para reprocessar coleta completa
UPDATE sync_control
SET last_watermark = '1970-01-01 00:00:00+00',
    last_run_status = 'manual_reset',
    last_run_at = NOW();

COMMIT;
