<?php
namespace PayCal\Core;

defined('ABSPATH') || exit;

use PayCal\Permissions\Access;

class AdminMenu {
  public static function register(): void {
    if (!class_exists(Access::class)) {
      \paycal_debug_log('[PayCal] Admin menu unavailable: permissions access class missing.');
      add_action('admin_notices', function () {
        if (current_user_can('manage_options')) {
          echo '<div class="notice notice-error"><p>' . esc_html__('PayCal admin menu could not load because the permissions layer is unavailable.', 'klamra-paycal-for-aspaclaria') . '</p></div>';
        }
      });
      return;
    }

    $items = [
      'dashboard' => [
        'page_title' => 'PayCal',
        'menu_title' => 'ברוכים הבאים',
        'slug' => 'paycal-dashboard',
        'callback' => [\PayCal\Admin\Page_Dashboard::class, 'render'],
      ],
      'connector' => [
        'page_title' => 'חיבור PayCal',
        'menu_title' => 'חיבור PayCal',
        'slug' => 'paycal-connector',
        'callback' => [\PayCal\Admin\Page_Connector::class, 'render'],
      ],
      'license' => [
        'page_title' => 'רישיון',
        'menu_title' => 'רישיון',
        'slug' => 'paycal-license',
        'callback' => [\PayCal\Admin\Page_License::class, 'render'],
      ],
      'bundles' => [
        'page_title' => 'ערכות',
        'menu_title' => 'ערכות',
        'slug' => 'paycal-bundles',
        'callback' => [\PayCal\Admin\Page_Marketplace::class, 'render'],
      ],
      'bit' => [
        'page_title' => 'ביט',
        'menu_title' => 'ביט',
        'slug' => 'paycal-bit',
        'callback' => [\PayCal\Admin\Page_Bit::class, 'render'],
      ],
      'invoices' => [
        'page_title' => 'חשבוניות',
        'menu_title' => 'חשבוניות',
        'slug' => 'paycal-invoices',
        'callback' => [\PayCal\Admin\Page_Invoices::class, 'render'],
      ],
      'permissions' => [
        'page_title' => 'הרשאות',
        'menu_title' => 'הרשאות',
        'slug' => 'paycal-permissions',
        'callback' => [\PayCal\Admin\Page_Permissions::class, 'render'],
      ],
      'setup' => [
        'page_title' => 'התקנה',
        'menu_title' => 'התקנה',
        'slug' => 'paycal-setup',
        'callback' => [\PayCal\Admin\Page_Wizard::class, 'render'],
      ],
    ];

    $visible = [];
    foreach ($items as $section => $item) {
      if (!is_array($item['callback']) || !class_exists($item['callback'][0]) || !is_callable($item['callback'])) {
        \paycal_debug_log('[PayCal] Admin page unavailable: ' . $section);
        continue;
      }
      if (Access::can($section)) {
        $visible[$section] = $item;
      }
    }

    if (empty($visible)) {
      return;
    }

    $first = reset($visible);
    $menu_slug = $first['slug'];

    add_menu_page(
      'PayCal',
      'PayCal',
      'read',
      $menu_slug,
      $first['callback'],
      'dashicons-money-alt',
      56
    );

    foreach ($visible as $item) {
      add_submenu_page(
        $menu_slug,
        $item['page_title'],
        $item['menu_title'],
        'read',
        $item['slug'],
        $item['callback']
      );
    }
  }
}
