-- Fix malformed caller numbers for Per Krag's Yealink phone.
--
-- Run via:
--   podman exec -i bmc-hub-postgres-prod psql -U bmc_hub -d hubdb_v2 -f /dev/stdin < fix_telefoni_per_krag_numbers.sql

BEGIN;

-- STEP 1: Fix any remaining numbers that still have the 1857220892 suffix
-- (threshold lowered from >15 to >10 to catch shorter corrupted numbers)
UPDATE telefoni_opkald
SET ekstern_nummer = CASE
    -- Too few remaining digits → NULL
    WHEN (length(regexp_replace(ekstern_nummer, '[^0-9]', '', 'g')) - 10) <= 3
        THEN NULL
    -- 8 remaining digits = Danish number → +45 prefix
    WHEN (length(regexp_replace(ekstern_nummer, '[^0-9]', '', 'g')) - 10) = 8
        THEN '+45' || left(regexp_replace(ekstern_nummer, '[^0-9]', '', 'g'),
                            length(regexp_replace(ekstern_nummer, '[^0-9]', '', 'g')) - 10)
    -- Other length → keep + prefix
    ELSE '+' || left(regexp_replace(ekstern_nummer, '[^0-9]', '', 'g'),
                     length(regexp_replace(ekstern_nummer, '[^0-9]', '', 'g')) - 10)
END
WHERE regexp_replace(ekstern_nummer, '[^0-9]', '', 'g') LIKE '%1857220892'
  AND length(regexp_replace(ekstern_nummer, '[^0-9]', '', 'g')) > 10;

-- STEP 2: Fix numbers that were previously half-patched: '+' + exactly 8 digits.
-- '+[8 digits]' is not valid E.164 (minimum 9 digits needed), so these are
-- Danish numbers that lost their +45 prefix during the first patch.
UPDATE telefoni_opkald
SET ekstern_nummer = '+45' || regexp_replace(ekstern_nummer, '[^0-9]', '', 'g')
WHERE ekstern_nummer ~ '^\+[0-9]{8}$';

-- Verify: show any remaining suspicious numbers for this user
SELECT id, started_at, ekstern_nummer
FROM telefoni_opkald t
JOIN users u ON u.user_id = t.bruger_id
WHERE u.full_name ILIKE '%per krag%'
ORDER BY started_at DESC
LIMIT 20;

COMMIT;


-- Preview what will be changed
SELECT
    id,
    ekstern_nummer AS before,
    CASE
        WHEN (length(regexp_replace(ekstern_nummer, '[^0-9]', '', 'g')) - 10) < 4
            THEN NULL
        WHEN (length(regexp_replace(ekstern_nummer, '[^0-9]', '', 'g')) - 10) = 8
            THEN '+45' || left(regexp_replace(ekstern_nummer, '[^0-9]', '', 'g'),
                                length(regexp_replace(ekstern_nummer, '[^0-9]', '', 'g')) - 10)
        WHEN ekstern_nummer LIKE '+%'
            THEN '+' || left(regexp_replace(ekstern_nummer, '[^0-9]', '', 'g'),
                              length(regexp_replace(ekstern_nummer, '[^0-9]', '', 'g')) - 10)
        ELSE left(regexp_replace(ekstern_nummer, '[^0-9]', '', 'g'),
                  length(regexp_replace(ekstern_nummer, '[^0-9]', '', 'g')) - 10)
    END AS after
FROM telefoni_opkald
WHERE regexp_replace(ekstern_nummer, '[^0-9]', '', 'g') LIKE '%1857220892'
  AND length(regexp_replace(ekstern_nummer, '[^0-9]', '', 'g')) > 15
ORDER BY started_at DESC;

-- Apply the fix
UPDATE telefoni_opkald
SET ekstern_nummer = CASE
    -- Strip last 10 digits (the local SIP suffix)
    WHEN (length(regexp_replace(ekstern_nummer, '[^0-9]', '', 'g')) - 10) < 4
        THEN NULL
    -- 8 remaining digits = Danish number → prefix +45
    WHEN (length(regexp_replace(ekstern_nummer, '[^0-9]', '', 'g')) - 10) = 8
        THEN '+45' || left(regexp_replace(ekstern_nummer, '[^0-9]', '', 'g'),
                            length(regexp_replace(ekstern_nummer, '[^0-9]', '', 'g')) - 10)
    -- Otherwise keep original + prefix
    WHEN ekstern_nummer LIKE '+%'
        THEN '+' || left(regexp_replace(ekstern_nummer, '[^0-9]', '', 'g'),
                          length(regexp_replace(ekstern_nummer, '[^0-9]', '', 'g')) - 10)
    ELSE left(regexp_replace(ekstern_nummer, '[^0-9]', '', 'g'),
              length(regexp_replace(ekstern_nummer, '[^0-9]', '', 'g')) - 10)
END
WHERE regexp_replace(ekstern_nummer, '[^0-9]', '', 'g') LIKE '%1857220892'
  AND length(regexp_replace(ekstern_nummer, '[^0-9]', '', 'g')) > 15;

SELECT concat('Retter ', count(*), ' rækker') AS resultat
FROM telefoni_opkald
WHERE regexp_replace(ekstern_nummer, '[^0-9]', '', 'g') LIKE '%1857220892'
  AND length(regexp_replace(ekstern_nummer, '[^0-9]', '', 'g')) > 15;

COMMIT;
