<?php
if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * Anbaarak | Best Inventory management plugin for WooCommerce.
 * All rights reserved for WP LEGEND team (wplegend.ir)
 */

 
function anbarak_get_app_tabs()
{
    return array(
        'inventory'        => [
            'name' => esc_html__('Products', 'anbarak'),
            'menu_name' => esc_html__('Products', 'anbarak'),
            'icon' => 'icon-box',
            'perms' => ['admin', 'manage_woocommerce']
        ],
        'finance'     => [
            'name' => esc_html__('Finance', 'anbarak'),
            'menu_name' => esc_html__('Finance', 'anbarak'),
            'icon' => 'icon-dollar-sign',
            'perms' => ['admin', 'manage_woocommerce'],
            'children' => [
                'expenses'     => [
                    'name' => esc_html__('Expenses', 'anbarak'),
                    'menu_name' => esc_html__('Expenses', 'anbarak'),
                    'icon' => 'icon-dollar-sign',
                    'perms' => ['admin', 'manage_woocommerce']
                ],
            ]
        ],
        'report'     => [
            'name' => esc_html__('Reports', 'anbarak'),
            'menu_name' => esc_html__('Reports', 'anbarak'),
            'icon' => 'icon-pie-chart',
            'perms' => ['admin', 'manage_woocommerce']
        ],
        'orders'     => [
            'name' => esc_html__('Orders', 'anbarak'),
            'menu_name' => esc_html__('Orders', 'anbarak'),
            'icon' => 'icon-archive',
            'perms' => ['admin', 'manage_woocommerce'],
        ],
        'settings'     => [
            'name' => esc_html__('Settings', 'anbarak'),
            'menu_name' => esc_html__('Settings', 'anbarak'),
            'icon' => 'icon-settings',
            'perms' => ['admin', 'manage_woocommerce']
        ],
    );
}

function anbarak_get_permitted_tabs()
{
    $all_tabs = anbarak_get_app_tabs();
    $permitted_tabs = [];
    
    foreach ($all_tabs as $tab_key => $tab_data) {
        if (anbarak_user_has_tab_permission($tab_data)) {
            $permitted_tabs[$tab_key] = $tab_data;
            
            if (isset($tab_data['children'])) {
                $permitted_children = [];
                
                foreach ($tab_data['children'] as $child_key => $child_data) {
                    if (anbarak_user_has_tab_permission($child_data)) {
                        $permitted_children[$child_key] = $child_data;
                    }
                }
                if (!empty($permitted_children)) {
                    $permitted_tabs[$tab_key]['children'] = $permitted_children;
                } else {
                    unset($permitted_tabs[$tab_key]['children']);
                }
            }
        }
    }
    
    return $permitted_tabs;
}


function anbarak_render_page_content($active_tab)
{

    $page_name = anbarak_get_current_page_name($active_tab);

    $tabs = anbarak_get_app_tabs();

    if (isset($tabs[$active_tab])) {
        $page_data = $tabs[$active_tab];

        if (!anbarak_user_has_tab_permission($page_data)) {
            echo '<div class="erroxr"><p>' . esc_html__('You do not have permission to access this page.', 'anbarak') . '</p></div>';
            return;
        }
    }


    switch ($active_tab) {
        case 'buys':
            require_once ANBARAK_VIEW_PATH . 'buys.php';
            break;
        case 'finance':
            require_once ANBARAK_VIEW_PATH . 'finance.php';
            break;
        case 'report':
            require_once ANBARAK_VIEW_PATH . 'report.php';
            break;
        case 'settings':
            require_once ANBARAK_VIEW_PATH . 'settings.php';
            break;
        case 'orders':
            require_once ANBARAK_VIEW_PATH . 'orders.php';
            break;
        default:
            require_once ANBARAK_VIEW_PATH . 'inventory.php';
            break;
    }
}

function anbarak_get_current_page_name($active_tab)
{
    $tabs = anbarak_get_app_tabs();
    return $tabs[$active_tab]['name'] ?? $active_tab;
}


function anbarak_nonce()
{
    echo '<input type="hidden" name="security" value="' . esc_attr(ANBARAK_AJAX_NONCE) . '">';
}
