-- Migration der Lizenzschlüssel vom alten Format zum neuen Format
-- Alt: AF-YYYYMMFT-XXXX-YYYY-ZZZZ
-- Neu: AF-F-YYYYMM-XXXX-YYYY-ZZZZ

-- Backup der aktuellen Schlüssel erstellen (für Sicherheit)
CREATE TEMP TABLE license_backup AS 
SELECT id, license_key FROM licenses;

-- Update für Fullversion Keys (F)
UPDATE licenses 
SET license_key = 
    CONCAT(
        SUBSTRING(license_key, 1, 3),  -- 'AF-'
        '-F-',
        SUBSTRING(license_key, 4, 6),   -- 'YYYYMM'
        '-',
        SUBSTRING(license_key, 11)      -- Rest des Keys
    )
WHERE license_key LIKE 'AF-%F-%'
  AND license_type = 'full'
  AND license_key NOT LIKE 'AF-F-%';  -- Nicht bereits migriert

-- Update für Testversion Keys (T)
UPDATE licenses 
SET license_key = 
    CONCAT(
        SUBSTRING(license_key, 1, 3),  -- 'AF-'
        '-T-',
        SUBSTRING(license_key, 4, 6),   -- 'YYYYMM'
        '-',
        SUBSTRING(license_key, 11)      -- Rest des Keys
    )
WHERE license_key LIKE 'AF-%T-%'
  AND license_type = 'test'
  AND license_key NOT LIKE 'AF-T-%';  -- Nicht bereits migriert

-- Zeige die Änderungen
SELECT 
    b.license_key as old_key,
    l.license_key as new_key,
    l.license_type
FROM licenses l
JOIN license_backup b ON l.id = b.id
WHERE b.license_key != l.license_key
ORDER BY l.id;

-- Anzahl der migrierten Keys
SELECT 
    COUNT(*) as total_migrated,
    SUM(CASE WHEN license_type = 'full' THEN 1 ELSE 0 END) as full_licenses,
    SUM(CASE WHEN license_type = 'test' THEN 1 ELSE 0 END) as test_licenses
FROM licenses l
JOIN license_backup b ON l.id = b.id
WHERE b.license_key != l.license_key;