<?php
/**
 * Business Type Configuration - BarberFlow SaaS
 * Configuracao centralizada para diferentes tipos de negocio
 *
 * business_type: 'service' (Barbearia/Salao) ou 'retail' (Loja/Varejo)
 */

/**
 * Retorna a configuracao completa baseada no business_type
 */
function getBusinessConfig($type = 'service'): array {
    $configs = [
        // ============================================
        // CONFIGURACAO PARA SERVICOS (Barbearia/Salao)
        // ============================================
        'service' => [
            'label' => 'Barbearia / Salão',
            'icon' => 'solar:scissors-bold',
            'primary_action' => [
                'label' => 'Novo Agendamento',
                'icon' => 'solar:calendar-add-bold',
                'url' => '/pages/agendamentos.php?action=new'
            ],
            'menu' => [
                'principal' => [
                    ['page' => 'dashboard', 'label' => 'Dashboard', 'icon' => 'solar:home-2-bold', 'url' => '/dashboard.php'],
                    ['page' => 'agenda', 'label' => 'Agenda', 'icon' => 'solar:calendar-bold', 'url' => '/pages/agenda.php'],
                    ['page' => 'agendamentos', 'label' => 'Agendamentos', 'icon' => 'solar:calendar-mark-bold', 'url' => '/pages/agendamentos.php'],
                    ['page' => 'servicos', 'label' => 'Serviços', 'icon' => 'solar:scissors-bold', 'url' => '/pages/servicos.php'],
                    ['page' => 'clientes', 'label' => 'Clientes', 'icon' => 'solar:users-group-rounded-bold', 'url' => '/pages/clientes.php'],
                    ['page' => 'equipe', 'label' => 'Equipe', 'icon' => 'solar:user-id-bold', 'url' => '/pages/equipe.php'],
                    ['page' => 'avaliacoes', 'label' => 'Avaliações', 'icon' => 'solar:star-bold', 'url' => '/pages/avaliacoes.php'],
                ],
                'financeiro' => [
                    ['page' => 'produtos', 'label' => 'Produtos', 'icon' => 'solar:bag-bold', 'url' => '/pages/produtos/'],
                    ['page' => 'financeiro', 'label' => 'Financeiro', 'icon' => 'solar:wallet-bold', 'url' => '/pages/financeiro.php'],
                    ['page' => 'relatorios', 'label' => 'Relatórios', 'icon' => 'solar:chart-bold', 'url' => '/pages/relatorios.php'],
                    ['page' => 'pagamentos', 'label' => 'Pagamentos', 'icon' => 'solar:card-bold', 'url' => '/pages/pagamentos.php'],
                ],
                'configuracoes' => [
                    ['page' => 'configuracoes', 'label' => 'Configurações', 'icon' => 'solar:settings-bold', 'url' => '/pages/configuracoes.php'],
                    ['page' => 'templates', 'label' => 'Templates', 'icon' => 'solar:palette-bold', 'url' => '/pages/templates.php'],
                    ['page' => 'minha-loja', 'label' => 'Minha Loja', 'icon' => 'solar:shop-bold', 'url' => '/pages/minha-loja.php'],
                    ['page' => 'frete', 'label' => 'Frete', 'icon' => 'solar:routing-2-bold', 'url' => '/pages/frete.php'],
                    ['page' => 'whatsapp', 'label' => 'WhatsApp', 'icon' => 'solar:chat-round-dots-bold', 'url' => '/pages/dashboard/whatsapp/whatsapp.php'],
                    ['page' => 'assinatura', 'label' => 'Assinatura', 'icon' => 'solar:crown-bold', 'url' => '/pages/assinatura.php'],
                ]
            ],
            'dashboard_kpis' => [
                ['key' => 'appointments_today', 'label' => 'Agendamentos Hoje', 'icon' => 'solar:calendar-bold', 'color' => '#CCA43B'],
                ['key' => 'revenue_today', 'label' => 'Receita Hoje', 'icon' => 'solar:wallet-bold', 'color' => '#22C55E'],
                ['key' => 'clients_active', 'label' => 'Clientes Ativos', 'icon' => 'solar:users-group-rounded-bold', 'color' => '#3B82F6'],
                ['key' => 'next_appointment', 'label' => 'Pendentes Hoje', 'icon' => 'solar:clock-circle-bold', 'color' => '#8B5CF6'],
            ],
            'allowed_pages' => ['dashboard', 'agenda', 'agendamentos', 'servicos', 'clientes', 'equipe', 'produtos', 'avaliacoes', 'financeiro', 'relatorios', 'pagamentos', 'configuracoes', 'templates', 'minha-loja', 'frete', 'assinatura', 'whatsapp'],
            'blocked_pages' => ['pedidos', 'catalogo', 'estoque', 'categorias'],
        ],

        // ============================================
        // CONFIGURACAO PARA VAREJO (Loja de Roupas)
        // ============================================
        'retail' => [
            'label' => 'Loja / Varejo',
            'icon' => 'solar:bag-bold',
            'primary_action' => [
                'label' => 'Novo Produto',
                'icon' => 'solar:add-circle-bold',
                'url' => '/pages/produtos/novo.php'
            ],
            'menu' => [
                'principal' => [
                    ['page' => 'dashboard', 'label' => 'Dashboard', 'icon' => 'solar:home-2-bold', 'url' => '/dashboard.php'],
                    ['page' => 'pedidos', 'label' => 'Pedidos', 'icon' => 'solar:clipboard-list-bold', 'url' => '/pages/pedidos.php'],
                    ['page' => 'produtos', 'label' => 'Produtos', 'icon' => 'solar:bag-bold', 'url' => '/pages/produtos/'],
                    ['page' => 'categorias', 'label' => 'Categorias', 'icon' => 'solar:tag-bold', 'url' => '/pages/categorias.php'],
                    ['page' => 'estoque', 'label' => 'Estoque', 'icon' => 'solar:box-bold', 'url' => '/pages/estoque.php'],
                    ['page' => 'clientes', 'label' => 'Clientes', 'icon' => 'solar:users-group-rounded-bold', 'url' => '/pages/clientes.php'],
                    ['page' => 'avaliacoes', 'label' => 'Avaliações', 'icon' => 'solar:star-bold', 'url' => '/pages/avaliacoes.php'],
                ],
                'financeiro' => [
                    ['page' => 'financeiro', 'label' => 'Financeiro', 'icon' => 'solar:wallet-bold', 'url' => '/pages/financeiro.php'],
                    ['page' => 'relatorios', 'label' => 'Relatórios', 'icon' => 'solar:chart-bold', 'url' => '/pages/relatorios.php'],
                    ['page' => 'pagamentos', 'label' => 'Pagamentos', 'icon' => 'solar:card-bold', 'url' => '/pages/pagamentos.php'],
                ],
                'configuracoes' => [
                    ['page' => 'configuracoes', 'label' => 'Configurações', 'icon' => 'solar:settings-bold', 'url' => '/pages/configuracoes.php'],
                    ['page' => 'templates', 'label' => 'Templates', 'icon' => 'solar:palette-bold', 'url' => '/pages/templates.php'],
                    ['page' => 'minha-loja', 'label' => 'Minha Loja', 'icon' => 'solar:shop-bold', 'url' => '/pages/minha-loja.php'],
                    ['page' => 'frete', 'label' => 'Frete', 'icon' => 'solar:routing-2-bold', 'url' => '/pages/frete.php'],
                    ['page' => 'whatsapp', 'label' => 'WhatsApp', 'icon' => 'solar:chat-round-dots-bold', 'url' => '/pages/dashboard/whatsapp/'],
                    ['page' => 'assinatura', 'label' => 'Assinatura', 'icon' => 'solar:crown-bold', 'url' => '/pages/assinatura.php'],
                ]
            ],
            'dashboard_kpis' => [
                ['key' => 'orders_pending', 'label' => 'Pedidos Pendentes', 'icon' => 'solar:clipboard-list-bold', 'color' => '#F59E0B'],
                ['key' => 'revenue_today', 'label' => 'Vendas Hoje', 'icon' => 'solar:wallet-bold', 'color' => '#22C55E'],
                ['key' => 'products_low_stock', 'label' => 'Estoque Baixo', 'icon' => 'solar:danger-triangle-bold', 'color' => '#EF4444'],
                ['key' => 'visitors_today', 'label' => 'Visitantes Hoje', 'icon' => 'solar:eye-bold', 'color' => '#3B82F6'],
            ],
            'allowed_pages' => ['dashboard', 'pedidos', 'produtos', 'categorias', 'estoque', 'clientes', 'avaliacoes', 'financeiro', 'relatorios', 'pagamentos', 'configuracoes', 'templates', 'minha-loja', 'frete', 'assinatura', 'whatsapp'],
            'blocked_pages' => ['agenda', 'agendamentos', 'servicos', 'equipe'],
        ]
    ];

    return $configs[$type] ?? $configs['service'];
}

/**
 * Verifica se uma pagina e permitida para o business_type
 */
function isPageAllowed($page, $type = 'service'): bool {
    $config = getBusinessConfig($type);
    return in_array($page, $config['allowed_pages']);
}

/**
 * Verifica se uma pagina e bloqueada e redireciona com erro 403
 */
function checkPageAccess($businessType = null): void {
    if ($businessType === null) {
        $businessType = $_SESSION['business_type'] ?? 'service';
    }

    $currentPage = basename($_SERVER['PHP_SELF'], '.php');
    $config = getBusinessConfig($businessType);

    if (in_array($currentPage, $config['blocked_pages'])) {
        http_response_code(403);
        include __DIR__ . '/../pages/403.php';
        exit;
    }
}

/**
 * Retorna os KPIs do dashboard baseado no business_type
 */
function getDashboardKPIs($type = 'service'): array {
    $config = getBusinessConfig($type);
    return $config['dashboard_kpis'];
}

/**
 * Retorna o menu baseado no business_type
 */
function getMenuItems($type = 'service'): array {
    $config = getBusinessConfig($type);
    return $config['menu'];
}

/**
 * Retorna a acao primaria baseada no business_type
 */
function getPrimaryAction($type = 'service'): array {
    $config = getBusinessConfig($type);
    return $config['primary_action'];
}

