-- Migration: Add Talpa Transmission ID to transmissions table
-- Date: 2026-01-16
-- Description: Adds column to store Talpa API transmission ID for updates

-- Add talpa_transmission_id column
ALTER TABLE transmissions 
ADD COLUMN talpa_transmission_id VARCHAR(100) NULL 
COMMENT 'Talpa API transmission ID for updates'
AFTER api_response;

-- Add index for faster lookups
ALTER TABLE transmissions
ADD INDEX idx_talpa_transmission_id (talpa_transmission_id);

-- Optional: Try to extract transmission_id from existing api_response JSON
-- This will only work if api_response contains the 'id' field
UPDATE transmissions 
SET talpa_transmission_id = JSON_UNQUOTE(JSON_EXTRACT(api_response, '$.id'))
WHERE api_status = 'synced' 
AND api_response IS NOT NULL
AND JSON_VALID(api_response)
AND JSON_EXTRACT(api_response, '$.id') IS NOT NULL;
