-- diag_dispatch_addr.sql — état des adresses/coords des Dispatch Jobs issus du pont legacy.
\timing on
-- 1) Champs « adresse » présents sur le doctype Dispatch Job ?
SELECT column_name FROM information_schema.columns
 WHERE table_name = 'tabDispatch Job'
   AND (column_name ILIKE '%address%' OR column_name ILIKE '%location%'
        OR column_name ILIKE '%civic%' OR column_name ILIKE '%street%'
        OR column_name ILIKE '%zip%' OR column_name ILIKE '%postal%' OR column_name ILIKE '%city%')
 ORDER BY 1;

-- 2) État des jobs issus du pont (legacy_ticket_id non vide)
SELECT
  count(*) AS total_bridge,
  count(*) FILTER (WHERE latitude IS NULL OR longitude IS NULL OR abs(coalesce(latitude,0)) < 1) AS sans_coords,
  count(*) FILTER (WHERE coalesce(service_location,'') = '') AS sans_service_location,
  count(*) FILTER (WHERE coalesce(service_location,'') = '' AND (latitude IS NULL OR abs(coalesce(latitude,0)) < 1)) AS ni_sl_ni_coords
FROM "tabDispatch Job"
WHERE coalesce(legacy_ticket_id,'') <> '';

-- 3) Échantillon de jobs sans coords (que voit-on dans le sujet ?)
SELECT name, legacy_ticket_id, left(subject, 80) AS subject, service_location,
       round(latitude::numeric,4) AS lat, round(longitude::numeric,4) AS lon, status
FROM "tabDispatch Job"
WHERE coalesce(legacy_ticket_id,'') <> '' AND (latitude IS NULL OR abs(coalesce(latitude,0)) < 1)
ORDER BY modified DESC LIMIT 12;
