-- ========================================
-- INSERIR USUÁRIOS PARA A EMPRESA Leader Tecnologia LTDA
-- ========================================

-- 1. Inserir Usuário 1: Elton Souza
INSERT INTO core_business.users (
    company_id,
    name,
    email,
    password_hash,
    type,
    status,
    phone,
    preferences,
    last_login,
    email_verified,
    phone_verified,
    two_factor_enabled,
    deleted,
    created_at,
    updated_at
) VALUES (
    (SELECT id FROM core_business.companies WHERE name = 'Leader Tecnologia LTDA' ORDER BY id DESC LIMIT 1),
    'Elton Souza',
    'elton.souza@leadertecnologia.com',
    -- Hash da senha: Leader@2025!Strong#Pass (usando crypt com salt bcrypt)
    crypt('Leader@2025!Strong#Pass', gen_salt('bf', 12)),
    'ADMIN',
    'ACTIVE',
    '+55 11 99876-5432',
    '{
        "language": "pt-BR",
        "timezone": "America/Sao_Paulo",
        "notifications": {
            "push": true,
            "email": true,
            "inApp": true
        },
        "profile": {
            "position": "CTO",
            "department": "Tecnologia",
            "bio": "Chief Technology Officer da Leader Tecnologia, especialista em arquitetura de software e liderança técnica",
            "social_links": {
                "linkedin": "https://linkedin.com/in/elton-souza",
                "github": "https://github.com/elton-souza"
            }
        },
        "dashboard": {
            "theme": "light",
            "layout": "default"
        }
    }',
    CURRENT_TIMESTAMP,
    true,
    false,
    false,
    false,
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP
);

-- 2. Inserir Usuário 2: Felipe Barbosa
INSERT INTO core_business.users (
    company_id,
    name,
    email,
    password_hash,
    type,
    status,
    phone,
    preferences,
    last_login,
    email_verified,
    phone_verified,
    two_factor_enabled,
    deleted,
    created_at,
    updated_at
) VALUES (
    (SELECT id FROM core_business.companies WHERE name = 'Leader Tecnologia LTDA' ORDER BY id DESC LIMIT 1),
    'Felipe Barbosa',
    'felipe.barbosa@leadertecnologia.com',
    -- Hash da senha: Leader@2025!Strong#Pass (usando crypt com salt bcrypt)
    crypt('Leader@2025!Strong#Pass', gen_salt('bf', 12)),
    'ADMIN',
    'ACTIVE',
    '+55 11 99765-4321',
    '{
        "language": "pt-BR",
        "timezone": "America/Sao_Paulo",
        "notifications": {
            "push": true,
            "email": true,
            "inApp": false
        },
        "profile": {
            "position": "Gerente de Projetos",
            "department": "Desenvolvimento",
            "bio": "Gerente de Projetos na Leader Tecnologia, especialista em metodologias ágeis e gestão de equipes de desenvolvimento",
            "social_links": {
                "linkedin": "https://linkedin.com/in/felipe-barbosa",
                "github": "https://github.com/felipe-barbosa"
            }
        },
        "dashboard": {
            "theme": "dark",
            "layout": "compact"
        }
    }',
    CURRENT_TIMESTAMP,
    true,
    false,
    false,
    false,
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP
);

-- ========================================
-- VERIFICAÇÃO DOS DADOS INSERIDOS
-- ========================================

-- Verificar a empresa Leader Tecnologia LTDA
SELECT 
    id,
    name,
    email,
    active_plan,
    status,
    max_users
FROM core_business.companies 
WHERE name = 'Leader Tecnologia LTDA';

-- Verificar os usuários criados
SELECT 
    u.id,
    u.name,
    u.email,
    u.type,
    u.status,
    c.name as company_name,
    u.phone,
    u.email_verified
FROM core_business.users u
JOIN core_business.companies c ON u.company_id = c.id
WHERE c.name = 'Leader Tecnologia LTDA'
ORDER BY u.id;

-- Testar login dos usuários (verificar senha hash)
SELECT 
    u.name,
    u.email,
    CASE 
        WHEN u.password_hash = crypt('Leader@2025!Strong#Pass', u.password_hash) 
        THEN 'Senha correta ✅'
        ELSE 'Senha incorreta ❌'
    END as password_test
FROM core_business.users u
JOIN core_business.companies c ON u.company_id = c.id
WHERE c.name = 'Leader Tecnologia LTDA';

-- Estatísticas finais
SELECT 
    'Total de empresas: ' || count(*) as info
FROM core_business.companies
UNION ALL
SELECT 
    'Total de usuários: ' || count(*) as info
FROM core_business.users
UNION ALL
SELECT 
    'Usuários da Leader Tecnologia LTDA: ' || count(*) as info
FROM core_business.users u
JOIN core_business.companies c ON u.company_id = c.id
WHERE c.name = 'Leader Tecnologia LTDA';
