-- ========================================
-- INSERIR NOVA EMPRESA: Leader Tecnologia LTDA
-- ========================================

-- 1. Inserir a empresa Leader Tecnologia
INSERT INTO core_business.companies (
    name,
    description,
    email,
    phone,
    website,
    address,
    active_plan,
    max_users,
    max_social_accounts,
    max_posts_per_month,
    max_storage_gb,
    features,
    status,
    registration_date,
    expiration_date,
    billing_info,
    settings,
    deleted,
    created_at,
    updated_at
) VALUES (
    'Leader Tecnologia',
    'Empresa especializada em desenvolvimento de software e soluções tecnológicas inovadoras',
    'contato@leadertecnologia.com',
    '+55 11 3456-7890',
    'https://www.leadertecnologia.com',
    '{
        "street": "Rua das Tecnologias, 123",
        "neighborhood": "Vila Olímpia",
        "city": "São Paulo",
        "state": "SP",
        "postal_code": "04551-000",
        "country": "Brasil",
        "complement": "Sala 456"
    }',
    'PROFESSIONAL',
    50,
    10,
    1000,
    100.0,
    '{
        "analytics": true,
        "automation": true,
        "collaboration": true,
        "advancedReports": true
    }',
    'ACTIVE',
    CURRENT_TIMESTAMP,
    '2026-08-22 23:59:59',
    '{
        "billing_address": {
            "street": "Rua das Tecnologias, 123",
            "city": "São Paulo",
            "state": "SP",
            "postal_code": "04551-000",
            "country": "Brasil"
        },
        "payment_method": "credit_card",
        "billing_cycle": "annual"
    }',
    '{
        "language": "pt-BR",
        "timezone": "America/Sao_Paulo",
        "notifications": {
            "push": true,
            "email": true
        },
        "social_media_preferences": {
            "auto_publish": false,
            "content_approval": true,
            "analytics_frequency": "weekly"
        }
    }',
    false,
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP
);

-- Obter o ID da empresa recém criada para usar nas próximas inserções
-- (Assumindo que será o próximo ID disponível)

-- 2. 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' 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
);

-- 3. 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' 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)),
    'MANAGER',
    '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 criada
SELECT 
    id,
    name,
    email,
    website,
    active_plan,
    status
FROM core_business.companies 
WHERE name = 'Leader Tecnologia';

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

-- Mostrar 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: ' || count(*) as info
FROM core_business.users u
JOIN core_business.companies c ON u.company_id = c.id
WHERE c.name = 'Leader Tecnologia';
