-- Script para configurar agentes de ejemplo en las colas
-- Fecha: 31 de Julio de 2025

-- Primero, limpiar queue_members de pruebas anteriores
DELETE FROM asterisk.queue_members 
WHERE queue_name IN ('tarot', 'info', 'erotico', 'redireccion');

-- 1. COLA TAROT - Mezcla de agentes locales y remotos
-- Agentes remotos (con teléfono)
INSERT INTO asterisk.queue_members 
(queue_name, interface, membername, state_interface, penalty, paused)
VALUES
-- Cristina (remoto)
('tarot', 'Local/324@agents-out', '324', 'PJSIP/324', 0, 0),
-- Paloma (remoto) 
('tarot', 'Local/325@agents-out', '325', 'PJSIP/325', 0, 0),
-- Tatiana (remoto)
('tarot', 'Local/326@agents-out', '326', 'PJSIP/326', 0, 0),
-- Clara (remoto)
('tarot', 'Local/301@agents-out', '301', 'PJSIP/301', 0, 0),
-- Bruno (remoto)
('tarot', 'Local/303@agents-out', '303', 'PJSIP/303', 0, 0);

-- Agentes locales (sin teléfono)
INSERT INTO asterisk.queue_members 
(queue_name, interface, membername, state_interface, penalty, paused)
VALUES
-- Carmen Luna (local)
('tarot', 'PJSIP/351', '351', 'PJSIP/351', 0, 0),
-- Pilar Vidente (local)
('tarot', 'PJSIP/352', '352', 'PJSIP/352', 0, 0),
-- Rosa Tarot (local)
('tarot', 'PJSIP/353', '353', 'PJSIP/353', 0, 0);

-- 2. COLA INFO - Principalmente locales
INSERT INTO asterisk.queue_members 
(queue_name, interface, membername, state_interface, penalty, paused)
VALUES
-- Agentes locales para info
('info', 'PJSIP/1001', '1001', 'PJSIP/1001', 0, 0),
('info', 'PJSIP/1002', '1002', 'PJSIP/1002', 0, 0),
('info', 'PJSIP/1003', '1003', 'PJSIP/1003', 0, 0);

-- 3. COLA EROTICO - Solo si hay agentes especializados
-- Por ahora vacía, agregar según necesidad

-- 4. Verificar configuración
SELECT 
    qm.queue_name as Cola,
    qm.membername as Codigo,
    a.nombre as Nombre,
    CASE 
        WHEN qm.interface LIKE 'Local/%' THEN 'Remoto'
        ELSE 'Local'
    END as Tipo,
    CASE 
        WHEN qm.interface LIKE 'Local/%' THEN a.telefono
        ELSE 'Extension directa'
    END as Destino,
    qm.paused as Pausado
FROM asterisk.queue_members qm
LEFT JOIN asterisk.agentes a ON qm.membername = a.usuario
WHERE qm.queue_name IN ('tarot', 'info', 'erotico', 'redireccion')
ORDER BY qm.queue_name, qm.membername;

-- Estadísticas
SELECT 
    queue_name as Cola,
    COUNT(*) as Total_Agentes,
    SUM(CASE WHEN interface LIKE 'Local/%' THEN 1 ELSE 0 END) as Remotos,
    SUM(CASE WHEN interface NOT LIKE 'Local/%' THEN 1 ELSE 0 END) as Locales
FROM asterisk.queue_members
WHERE queue_name IN ('tarot', 'info', 'erotico', 'redireccion')
GROUP BY queue_name;