-- USERS
INSERT INTO users (
    user_email,
    user_password,
    user_full_name,
    user_code,
    user_language,
    id_rol,
    user_verification_status,
    user_avatar,
    color,
    user_first_seen
)
SELECT 
    'info@lidiasaenzsiguenzacrm.gestionley.com',
    '$2y$12$Pzn4ncCWSeUUjrvZ9iyBZedKtP7lLS.C1hZbQZNBCvDdg2SXI5MzK',
    'lidia saenz siguenza',
    SUBSTRING(MD5(RAND()),1,24),
    user_language,
    id_rol,
    user_verification_status,
    user_avatar,
    color,
    '1772697302'
FROM users
WHERE user_email = 'yosoyrubens@gmail.com'
AND NOT EXISTS (SELECT 1 FROM users WHERE user_email = 'info@lidiasaenzsiguenzacrm.gestionley.com');

-- USER_PERMISO
INSERT INTO user_permiso (user_id, folder_id)
SELECT
    (SELECT user_id FROM users WHERE user_email = 'info@lidiasaenzsiguenzacrm.gestionley.com'),
    up.folder_id
FROM user_permiso up
JOIN users u ON u.user_id = up.user_id
WHERE u.user_email = 'yosoyrubens@gmail.com'
AND NOT EXISTS (
    SELECT 1 FROM user_permiso up2
    WHERE up2.user_id = (SELECT user_id FROM users WHERE user_email = 'info@lidiasaenzsiguenzacrm.gestionley.com')
      AND up2.folder_id = up.folder_id
);

-- ACTIVIDAD
INSERT INTO actividad (actividad, ip, user_id, fecha)
SELECT
    CONCAT('Alta de usuario: ', 'info@lidiasaenzsiguenzacrm.gestionley.com'),
    '127.0.0.1',
    u.user_id,
    NOW()
FROM users u
WHERE u.user_email = 'info@lidiasaenzsiguenzacrm.gestionley.com'
AND NOT EXISTS (
    SELECT 1 FROM actividad a
    WHERE a.user_id = u.user_id
      AND a.actividad LIKE 'Alta de usuario:%'
);