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

class Manfiet_Expense_Admin_Menu {

    private $migration_manager;

    public function __construct() {
        // Instantiate the migration manager here so it registers its admin-post handlers early.
        $this->migration_manager = new Manfiet_Migration_Manager();
        add_action('admin_menu', [$this, 'register_admin_menu']);
        add_action('admin_bar_menu', [$this, 'add_admin_bar_menu_item'], 100);
    }

    private function can_manage_expenses() {
        return current_user_can('manage_expenses') || current_user_can('manage_options');
    }

    public function register_admin_menu() {
      if ($this->can_manage_expenses()) {
          add_menu_page(
              esc_html__( 'Expense Tracker', 'manfi-expense-tracker' ), // Page title
              esc_html__( 'Expense Tracker', 'manfi-expense-tracker' ), // Menu title
              'manage_options', // Capability
              'manfi-expense-tracker', // Menu slug
              [$this, 'admin_dashboard'], // Callback function
              MANFIET_EXPENSE_TRACKER_PLUGIN_URL . 'assets/img/icon.svg?v=' . MANFIET_EXPENSE_TRACKER_VERSION // Icon
          );

          add_submenu_page(
              'manfi-expense-tracker',
              esc_html__('All Expenses', 'manfi-expense-tracker'),
              esc_html__('All Expenses', 'manfi-expense-tracker'),
              'manage_options',
              'manfi-expense-tracker',
              [$this, 'admin_dashboard']
          );

          add_submenu_page(
              'manfi-expense-tracker',
              esc_html__('Add Expense', 'manfi-expense-tracker'),
              esc_html__('Add Expense', 'manfi-expense-tracker'),
              'manage_options',
              'manfiet-expense-add',
              [$this, 'add_expense_page']
          );

          add_submenu_page(
              'manfi-expense-tracker',
              esc_html__('Expense Categories', 'manfi-expense-tracker'),
              esc_html__('Expense Categories', 'manfi-expense-tracker'),
              'manage_options',
              'manfiet-expense-categories',
              [$this, 'category_dashboard']
          );

          add_submenu_page(
              'manfi-expense-tracker',
              esc_html__('Expense Tags', 'manfi-expense-tracker'),
              esc_html__('Expense Tags', 'manfi-expense-tracker'),
              'manage_options',
              'manfiet-expense-tags',
              [$this, 'tag_dashboard']
          );

          add_submenu_page(
              'manfi-expense-tracker',
              esc_html__('Budget Management', 'manfi-expense-tracker'),
              esc_html__('Budget Management', 'manfi-expense-tracker'),
              'manage_options',
              'manfiet-expense-budget',
              [$this, 'budget_dashboard']
          );

          add_submenu_page(
              'manfi-expense-tracker',
              esc_html__('Reports', 'manfi-expense-tracker'),
              esc_html__('Reports', 'manfi-expense-tracker'),
              'manage_options',
              'manfiet-expense-reports',
              [$this, 'reports_dashboard']
          );

          $this->migration_manager->register_submenu('manfi-expense-tracker');

          if (get_option('manfiet_enable_invoice_manager', 0)) {
              add_menu_page(
                  esc_html__('Invoice Manager', 'manfi-expense-tracker'),
                  esc_html__('Invoice Manager', 'manfi-expense-tracker'),
                  'manage_options',
                  'manfiet-invoice-manager',
                  [$this, 'invoice_manager_dashboard'],
                  'dashicons-media-text'
              );

              add_submenu_page(
                  'manfiet-invoice-manager',
                  esc_html__('Invoice Dashboard', 'manfi-expense-tracker'),
                  esc_html__('Invoice Dashboard', 'manfi-expense-tracker'),
                  'manage_options',
                  'manfiet-invoice-manager',
                  [$this, 'invoice_manager_dashboard']
              );

              add_submenu_page(
                  'manfiet-invoice-manager',
                  esc_html__('Create Invoice', 'manfi-expense-tracker'),
                  esc_html__('Create Invoice', 'manfi-expense-tracker'),
                  'manage_options',
                  'manfiet-invoice-create',
                  [$this, 'invoice_create_dashboard']
              );

              add_submenu_page(
                  'manfiet-invoice-manager',
                  esc_html__('Financial Accounts', 'manfi-expense-tracker'),
                  esc_html__('Financial Accounts', 'manfi-expense-tracker'),
                  'manage_options',
                  'manfiet-financial-accounts',
                  [$this, 'financial_accounts_dashboard']
              );

              add_submenu_page(
                  'manfiet-invoice-manager',
                  esc_html__('Create Financial Account', 'manfi-expense-tracker'),
                  esc_html__('Create Financial Account', 'manfi-expense-tracker'),
                  'manage_options',
                  'manfiet-financial-accounts-create',
                  [$this, 'financial_accounts_create_dashboard']
              );

              add_submenu_page(
                  'manfiet-invoice-manager',
                  esc_html__('Vendor Manager', 'manfi-expense-tracker'),
                  esc_html__('Vendor Manager', 'manfi-expense-tracker'),
                  'manage_options',
                  'manfiet-vendors',
                  [$this, 'vendors_dashboard']
              );

              add_submenu_page(
                  'manfiet-invoice-manager',
                  esc_html__('Create Vendor', 'manfi-expense-tracker'),
                  esc_html__('Create Vendor', 'manfi-expense-tracker'),
                  'manage_options',
                  'manfiet-vendors-create',
                  [$this, 'vendors_create_dashboard']
              );

              add_submenu_page(
                  'manfiet-invoice-manager',
                  esc_html__('Client Manager', 'manfi-expense-tracker'),
                  esc_html__('Client Manager', 'manfi-expense-tracker'),
                  'manage_options',
                  'manfiet-clients',
                  [$this, 'clients_dashboard']
              );

              add_submenu_page(
                  'manfiet-invoice-manager',
                  esc_html__('Create Client', 'manfi-expense-tracker'),
                  esc_html__('Create Client', 'manfi-expense-tracker'),
                  'manage_options',
                  'manfiet-clients-create',
                  [$this, 'clients_create_dashboard']
              );
          }
      } else {
          add_menu_page(
              esc_html__( 'My Expenses', 'manfi-expense-tracker' ), // Page title
              esc_html__( 'My Expenses', 'manfi-expense-tracker' ), // Menu title
              'read', // Capability
              'manfi-expense-tracker', // Menu slug
              [$this, 'user_dashboard'], // Callback function
              MANFIET_EXPENSE_TRACKER_PLUGIN_URL . 'assets/img/icon.svg?v=' . MANFIET_EXPENSE_TRACKER_VERSION // Icon
          );
      }
  }

    public function admin_dashboard() {
      echo '<div class="wrap">';
      echo '<h2 class="wp-header-end" style="display:none;"></h2>';
      echo '<div id="manfi-expense-tracker"></div>';
      echo '</div>';
    }

    public function add_expense_page() {
      echo '<div class="wrap">';
      echo '<h2 class="wp-header-end" style="display:none;"></h2>';
      echo '<div id="manfi-expense-tracker"></div>';
      echo '</div>';
    }

    public function user_dashboard() {
      echo '<div class="wrap">';
      echo '<h2 class="wp-header-end" style="display:none;"></h2>';
      echo '<h1>' . esc_html__( 'My Expenses', 'manfi-expense-tracker' ) . '</h1>';
      echo '<div id="manfi-expense-tracker"></div>';
      echo '</div>';
    }

        public function category_dashboard() {
            // Allow administrators and manage_expenses capable users
            // if (!current_user_can( 'manage_options' )) {
            //     wp_die(esc_html__('You do not have permission to manage expense categories.', 'manfi-expense-tracker'));
            // }
            
            echo '<div class="wrap">';
            echo '<h2 class="wp-header-end" style="display:none;"></h2>';
            echo '<div id="manfi-expense-tracker"></div>';
            echo '</div>';
        }

        public function tag_dashboard() {
            if (!$this->can_manage_expenses()) {
                wp_die(esc_html__('You do not have permission to manage tags.', 'manfi-expense-tracker'));
            }

            echo '<div class="wrap">';
            echo '<h2 class="wp-header-end" style="display:none;"></h2>';
            echo '<div id="manfi-expense-tracker"></div>';
            echo '</div>';
        }

        public function budget_dashboard() {
            if (!$this->can_manage_expenses()) {
                wp_die(esc_html__('You do not have permission to manage budgets.', 'manfi-expense-tracker'));
            }

            echo '<div class="wrap">';
            echo '<h2 class="wp-header-end" style="display:none;"></h2>';
            echo '<div id="manfi-expense-tracker"></div>';
            echo '</div>';
        }

        public function reports_dashboard() {
            if (!$this->can_manage_expenses()) {
                wp_die(esc_html__('You do not have permission to view reports.', 'manfi-expense-tracker'));
            }

            echo '<div class="wrap">';
            echo '<h2 class="wp-header-end" style="display:none;"></h2>';
            echo '<div id="manfi-expense-tracker"></div>';
            echo '</div>';
        }

        public function invoice_manager_dashboard() {
            if (!$this->can_manage_expenses()) {
                wp_die(esc_html__('You do not have permission to manage invoices.', 'manfi-expense-tracker'));
            }

            echo '<div class="wrap">';
            echo '<h2 class="wp-header-end" style="display:none;"></h2>';
            echo '<div id="manfi-expense-tracker"></div>';
            echo '</div>';
        }

        public function invoice_create_dashboard() {
            if (!$this->can_manage_expenses()) {
                wp_die(esc_html__('You do not have permission to manage invoices.', 'manfi-expense-tracker'));
            }

            echo '<div class="wrap">';
            echo '<h2 class="wp-header-end" style="display:none;"></h2>';
            echo '<div id="manfi-expense-tracker"></div>';
            echo '</div>';
        }

        public function financial_accounts_dashboard() {
            if (!$this->can_manage_expenses()) {
                wp_die(esc_html__('You do not have permission to manage financial accounts.', 'manfi-expense-tracker'));
            }

            echo '<div class="wrap">';
            echo '<h2 class="wp-header-end" style="display:none;"></h2>';
            echo '<h1>' . esc_html__('Financial Accounts', 'manfi-expense-tracker') . '</h1>';
            echo '<div id="manfi-expense-tracker"></div>';
            echo '</div>';
        }

        public function financial_accounts_create_dashboard() {
            if (!$this->can_manage_expenses()) {
                wp_die(esc_html__('You do not have permission to manage financial accounts.', 'manfi-expense-tracker'));
            }

            echo '<div class="wrap">';
            echo '<h2 class="wp-header-end" style="display:none;"></h2>';
            echo '<div id="manfi-expense-tracker"></div>';
            echo '</div>';
        }

        public function vendors_dashboard() {
            if (!$this->can_manage_expenses()) {
                wp_die(esc_html__('You do not have permission to manage vendors.', 'manfi-expense-tracker'));
            }

            echo '<div class="wrap">';
            echo '<h2 class="wp-header-end" style="display:none;"></h2>';
            echo '<h1>' . esc_html__('Vendors', 'manfi-expense-tracker') . '</h1>';
            echo '<div id="manfi-expense-tracker"></div>';
            echo '</div>';
        }

        public function vendors_create_dashboard() {
            if (!$this->can_manage_expenses()) {
                wp_die(esc_html__('You do not have permission to manage vendors.', 'manfi-expense-tracker'));
            }

            echo '<div class="wrap">';
            echo '<h2 class="wp-header-end" style="display:none;"></h2>';
            echo '<div id="manfi-expense-tracker"></div>';
            echo '</div>';
        }

        public function clients_dashboard() {
            if (!$this->can_manage_expenses()) {
                wp_die(esc_html__('You do not have permission to manage clients.', 'manfi-expense-tracker'));
            }

            echo '<div class="wrap">';
            echo '<h2 class="wp-header-end" style="display:none;"></h2>';
            echo '<h1>' . esc_html__('Clients', 'manfi-expense-tracker') . '</h1>';
            echo '<div id="manfi-expense-tracker"></div>';
            echo '</div>';
        }

        public function clients_create_dashboard() {
            if (!$this->can_manage_expenses()) {
                wp_die(esc_html__('You do not have permission to manage clients.', 'manfi-expense-tracker'));
            }

            echo '<div class="wrap">';
            echo '<h2 class="wp-header-end" style="display:none;"></h2>';
            echo '<div id="manfi-expense-tracker"></div>';
            echo '</div>';
        }

    public function add_admin_bar_menu_item($wp_admin_bar) {
        if (!current_user_can('manage_expenses') && !current_user_can('manage_options') && !current_user_can('read')) {
            return;
        }

        $icon_url = MANFIET_EXPENSE_TRACKER_PLUGIN_URL . 'assets/img/icon.svg';
        $title = sprintf(
            '<img src="%1$s" style="width: 16px; height: 16px; vertical-align: middle;" alt="Icon" />',
            esc_url($icon_url)
        );

        $wp_admin_bar->add_node([
            'id'    => 'manfi-expense-tracker-admin-bar',
            'title' => $title,
            'href'  => admin_url('admin.php?page=manfi-expense-tracker'),
            'meta'  => [
                'title' => esc_html__('Expense Tracker', 'manfi-expense-tracker'),
            ]
        ]);
    }
}
