-- ========================================
-- ATUALIZAR USUÁRIO: Nicolas Palma (ID: 1)
-- ========================================

-- Primeiro, vamos verificar o usuário atual
SELECT 
    id,
    name,
    email,
    type,
    status,
    phone,
    email_verified,
    preferences
FROM core_business.users 
WHERE id = 4;  -- Corrigindo para ID 4 que é o Nicolas atual

-- Atualizar os dados do Nicolas Palma
UPDATE core_business.users 
SET 
    name = 'Nicolas Palma',
    email = 'nicolas.palma@lphubbermed.com.br',
    password_hash = crypt('HubberMed@2025#Strong!Pass', gen_salt('bf', 12)),
    type = 'COMPANY_ADMIN',
    status = 'ACTIVE',
    phone = '+55 11 99123-4567',
    email_verified = true,
    phone_verified = true,
    preferences = '{
        "language": "pt-BR",
        "timezone": "America/Sao_Paulo",
        "notifications": {
            "push": true,
            "email": true,
            "inApp": true
        },
        "profile": {
            "position": "CEO e Fundador",
            "department": "Diretoria",
            "bio": "CEO e Fundador da Hubber Med, líder visionário no setor de tecnologia médica e inovação em saúde",
            "social_links": {
                "linkedin": "https://linkedin.com/in/nicolas-palma",
                "twitter": "https://twitter.com/nicolaspalma"
            }
        },
        "permissions": {
            "can_manage_users": true,
            "can_manage_company": true,
            "can_access_analytics": true,
            "can_manage_billing": true,
            "can_manage_integrations": true,
            "can_view_audit_logs": true,
            "admin_level": "COMPANY_ADMIN"
        },
        "dashboard": {
            "theme": "light",
            "layout": "executive",
            "widgets": ["analytics", "users", "billing", "notifications"]
        },
        "security": {
            "two_factor_enabled": false,
            "session_timeout": 480,
            "ip_whitelist": []
        }
    }',
    updated_at = CURRENT_TIMESTAMP
WHERE id = 4;

-- Verificar a atualização
SELECT 
    u.id,
    u.name,
    u.email,
    u.type,
    u.status,
    u.phone,
    u.email_verified,
    u.phone_verified,
    c.name as company_name,
    u.preferences->>'profile'->>'position' as position,
    CASE 
        WHEN u.password_hash = crypt('HubberMed@2025#Strong!Pass', u.password_hash) 
        THEN 'Senha correta ✅'
        ELSE 'Senha incorreta ❌'
    END as password_test,
    u.updated_at
FROM core_business.users u
JOIN core_business.companies c ON u.company_id = c.id
WHERE u.id = 4;

-- Verificar permissões configuradas
SELECT 
    name,
    email,
    type,
    preferences->'permissions'->>'can_manage_users' as can_manage_users,
    preferences->'permissions'->>'can_manage_company' as can_manage_company,
    preferences->'permissions'->>'can_access_analytics' as can_access_analytics,
    preferences->'permissions'->>'admin_level' as admin_level,
    preferences->'profile'->>'position' as position
FROM core_business.users 
WHERE id = 4;

-- Mostrar configurações de idioma e fuso horário
SELECT 
    name,
    email,
    preferences->>'language' as language,
    preferences->>'timezone' as timezone,
    preferences->'dashboard'->>'theme' as theme,
    preferences->'dashboard'->>'layout' as layout
FROM core_business.users 
WHERE id = 4;

-- Estatísticas finais
SELECT 'Usuário Nicolas Palma atualizado com sucesso! ✅' as status;
