-- =============================================================================
-- Migracion: Agregar 12 columnas de tracking para Embedded Signup de Meta
-- Tabla: "1234567890" (numeros de WhatsApp Business)
-- Fecha: 2026-01-20
-- Autor: Claude Code (backend-agent)
-- =============================================================================

-- Grupo 1: Tracking del proceso de registro
-- -----------------------------------------------------------------------------
ALTER TABLE "1234567890" ADD COLUMN IF NOT EXISTS signup_started_at timestamp;
COMMENT ON COLUMN "1234567890".signup_started_at IS 'Marca cuando el usuario inicio el proceso de Embedded Signup';

ALTER TABLE "1234567890" ADD COLUMN IF NOT EXISTS session_info_received_at timestamp;
COMMENT ON COLUMN "1234567890".session_info_received_at IS 'Marca cuando se recibio la informacion de sesion del SDK de Facebook';

ALTER TABLE "1234567890" ADD COLUMN IF NOT EXISTS registration_attempts integer DEFAULT 0;
COMMENT ON COLUMN "1234567890".registration_attempts IS 'Contador de intentos de registro (para rate limiting)';

ALTER TABLE "1234567890" ADD COLUMN IF NOT EXISTS last_registration_attempt_at timestamp;
COMMENT ON COLUMN "1234567890".last_registration_attempt_at IS 'Marca del ultimo intento de registro';

-- Grupo 2: Tracking de errores
-- -----------------------------------------------------------------------------
ALTER TABLE "1234567890" ADD COLUMN IF NOT EXISTS last_error_code integer;
COMMENT ON COLUMN "1234567890".last_error_code IS 'Codigo numerico del ultimo error de la API de Meta';

ALTER TABLE "1234567890" ADD COLUMN IF NOT EXISTS last_error_message varchar(500);
COMMENT ON COLUMN "1234567890".last_error_message IS 'Mensaje descriptivo del ultimo error';

ALTER TABLE "1234567890" ADD COLUMN IF NOT EXISTS last_error_at timestamp;
COMMENT ON COLUMN "1234567890".last_error_at IS 'Cuando ocurrio el ultimo error';

-- Grupo 3: Tracking de webhooks
-- -----------------------------------------------------------------------------
ALTER TABLE "1234567890" ADD COLUMN IF NOT EXISTS webhook_subscribed_at timestamp;
COMMENT ON COLUMN "1234567890".webhook_subscribed_at IS 'Cuando se suscribio exitosamente a webhooks';

ALTER TABLE "1234567890" ADD COLUMN IF NOT EXISTS last_webhook_event varchar(100);
COMMENT ON COLUMN "1234567890".last_webhook_event IS 'Tipo del ultimo evento webhook recibido';

ALTER TABLE "1234567890" ADD COLUMN IF NOT EXISTS last_webhook_at timestamp;
COMMENT ON COLUMN "1234567890".last_webhook_at IS 'Cuando llego el ultimo webhook';

-- Grupo 4: Metadata de API
-- -----------------------------------------------------------------------------
ALTER TABLE "1234567890" ADD COLUMN IF NOT EXISTS api_call_count integer DEFAULT 0;
COMMENT ON COLUMN "1234567890".api_call_count IS 'Contador de llamadas a la API de Meta (para debugging)';

ALTER TABLE "1234567890" ADD COLUMN IF NOT EXISTS last_api_success_at timestamp;
COMMENT ON COLUMN "1234567890".last_api_success_at IS 'Cuando fue la ultima respuesta exitosa de Meta';

-- =============================================================================
-- FIN DE MIGRACION
-- =============================================================================
