-- diag_dispatch_addr2.sql — état du champ address + coords SL pour le job actif sans coords.
\timing on
-- 1) Remplissage du champ address sur les jobs du pont
SELECT count(*) FILTER (WHERE coalesce(address,'') = '') AS addr_vide,
       count(*) FILTER (WHERE coalesce(address,'') <> '') AS addr_ok,
       count(*) AS total
FROM "tabDispatch Job" WHERE coalesce(legacy_ticket_id,'') <> '';

-- 2) Le job actif sans coords (LEG-187195) : la SL liée a-t-elle des coords ?
SELECT sl.name, sl.address_line, sl.city,
       round(sl.latitude::numeric,5) AS lat, round(sl.longitude::numeric,5) AS lon
FROM "tabService Location" sl WHERE sl.name = 'LOC-0000002656';

-- 3) Parmi les jobs sans coords : combien sont actifs (open/assigned) vs terminés ?
SELECT status, count(*) FROM "tabDispatch Job"
 WHERE coalesce(legacy_ticket_id,'') <> '' AND (latitude IS NULL OR abs(coalesce(latitude,0)) < 1)
 GROUP BY status ORDER BY 2 DESC;

-- 4) Parmi les jobs SANS service_location : combien actifs ? (ce sont eux qui perdent l'adresse)
SELECT status, count(*) FROM "tabDispatch Job"
 WHERE coalesce(legacy_ticket_id,'') <> '' AND coalesce(service_location,'') = ''
 GROUP BY status ORDER BY 2 DESC;
