<?php

namespace BookWellAB;

defined('ABSPATH') || exit;

if (!class_exists('BookWellAB_Adminmenu')) {

    class BookWellAB_Adminmenu
    {


        public static function init()
        {
            add_action('admin_menu', [self::class, 'admin_menu_pages']);
            add_action('admin_enqueue_scripts', [self::class, 'enqueue_admin_menu_icon_styles'], 5);
            add_filter('plugin_action_links_' . plugin_basename(BOOKWELLAB_FILE), [self::class, 'plugin_action_links']);
        }

        /**
         * Enqueue submenu icon styles for the Book Well Now admin menu.
         *
         * @param string $hook_suffix Current admin screen hook suffix.
         * @return void
         */
        public static function enqueue_admin_menu_icon_styles($hook_suffix = '')
        {
            if ($hook_suffix !== '' && strpos($hook_suffix, 'bookwell') === false) {
                return;
            }

            $menu_icon_css = BOOKWELLAB_PATH . 'assets/css/admin-menu-icon.css';
            $menu_icon_ver = BOOKWELLAB_VERSION;
            if (file_exists($menu_icon_css)) {
                $menu_icon_ver .= '.' . filemtime($menu_icon_css);
            }

            wp_enqueue_style(
                'bookwellab-admin-menu-icon',
                BOOKWELLAB_URL . 'assets/css/admin-menu-icon.css',
                [],
                $menu_icon_ver
            );
        }

        public static function admin_menu_pages()
        {
            $menu_icon = BOOKWELLAB_URL . 'assets/images/menu-icon.png';
            if (!file_exists(BOOKWELLAB_PATH . 'assets/images/menu-icon.png')) {
                $menu_icon = 'dashicons-calendar-alt';
            }

            add_menu_page(
                __('Book Well Now', 'bookwellnow-appointment-booking'),
                __('Book Well Now', 'bookwellnow-appointment-booking'),
                'manage_options',
                'bookwell',
                [\BookWellAB\BOOKWELL::class, 'render'],
                $menu_icon,
                30
            );

            add_submenu_page(
                'bookwell',
                __('Dashboard', 'bookwellnow-appointment-booking'),
                __('Dashboard', 'bookwellnow-appointment-booking'),
                'manage_options',
                'bookwell',
                [\BookWellAB\BOOKWELL::class, 'render']
            );

            add_submenu_page(
                'bookwell',
                __('Calendar', 'bookwellnow-appointment-booking'),
                __('Calendar', 'bookwellnow-appointment-booking'),
                'bookwellab_manage_appointments', // Using the same capability as appointments
                'bookwell-calendar',
                [\BookWellAB\BOOKWELL::class, 'render']
            );

            add_submenu_page(
                'bookwell',
                __('Appointments', 'bookwellnow-appointment-booking'),
                __('Appointments', 'bookwellnow-appointment-booking'),
                'bookwellab_manage_appointments',
                'bookwell-appointments',
                [\BookWellAB\BOOKWELL::class, 'render']
            );

            add_submenu_page(
                'bookwell',
                __('Customers', 'bookwellnow-appointment-booking'),
                __('Customers', 'bookwellnow-appointment-booking'),
                'bookwellab_manage_customers',
                'bookwell-customers',
                [\BookWellAB\BOOKWELL::class, 'render']
            );

            add_submenu_page(
                'bookwell',
                __('Services', 'bookwellnow-appointment-booking'),
                __('Services', 'bookwellnow-appointment-booking'),
                'bookwellab_manage_services',
                'bookwell-services',
                [\BookWellAB\BOOKWELL::class, 'render']
            );

            do_action('bookwellab_register_admin_submenus');

            add_submenu_page(
                'bookwell',
                __('Staffs', 'bookwellnow-appointment-booking'),
                __('Staffs', 'bookwellnow-appointment-booking'),
                'bookwellab_manage_staff',
                'bookwell-staff',
                [\BookWellAB\BOOKWELL::class, 'render']
            );


            add_submenu_page(
                'bookwell',
                __('Notifications', 'bookwellnow-appointment-booking'),
                __('Notifications', 'bookwellnow-appointment-booking'),
                'bookwellab_manage_settings',
                'bookwell-notifications',
                [\BookWellAB\BOOKWELL::class, 'render']
            );

            add_submenu_page(
                'bookwell',
                __('Form Fields', 'bookwellnow-appointment-booking'),
                __('Form Fields', 'bookwellnow-appointment-booking'),
                'bookwellab_manage_settings',
                'bookwell-form-fields',
                [\BookWellAB\BOOKWELL::class, 'render']
            );

            add_submenu_page(
                'bookwell',
                __('Settings', 'bookwellnow-appointment-booking'),
                __('Settings', 'bookwellnow-appointment-booking'),
                'bookwellab_manage_settings',
                'bookwell-settings',
                [\BookWellAB\BOOKWELL::class, 'render']
            );

            do_action('bookwellab_register_admin_submenus_after_settings');

            add_submenu_page(
                'bookwell',
                __('Shortcode Generator', 'bookwellnow-appointment-booking'),
                __('Shortcode Generator', 'bookwellnow-appointment-booking'),
                'manage_options',
                'bookwell-shortcode-generator',
                [\BookWellAB\BOOKWELL::class, 'render']
            );


        }

        public static function plugin_action_links($links)
        {
            $settings_link = sprintf('<a href="%s">%s</a>', admin_url('admin.php?page=bookwell-settings'), __('Settings', 'bookwellnow-appointment-booking'));
            array_unshift($links, $settings_link);
            return $links;
        }




    }


}

