<?php
/**
 * AdminInit
 *
 * @class AdminInit
 * @package KimaAI\Admin
 * @since 1.0.0
 */

declare(strict_types=1);

namespace KimaAI\Admin;

defined('ABSPATH') || exit;

class AdminInit {

  /**
   * Registers admin hooks.
   */
  public function registerHooks(): void {
    add_action('admin_menu', [$this, 'adminMenu']);
    add_filter('plugin_action_links_' . KIMAAI_PLUGIN_BASENAME, [$this, 'pluginActionLinks']);
    // Add custom style for KimaAI logo
    add_action('admin_head', function () {
      echo '<style>
        #toplevel_page_kimaai {
          padding: 5px 0 !important;
          margin: 10px 0 !important;
        }
        #toplevel_page_kimaai a.wp-has-current-submenu {
         background: #6767fd !important;
         padding: 5px 0 !important;
        }
        #toplevel_page_kimaai .wp-menu-image img {
          opacity: 0.8;
          padding-top: 6px;
        }
        #toplevel_page_kimaai.wp-menu-open .wp-menu-image img {
          padding-left: 10px;
        }
      </style>';
    });
  }

  /**
   * Adds the admin menu.
   */
  public function adminMenu(): void {
    $mini_logo = KIMAAI_PLUGIN_URL . 'assets/images/mini-logo.svg';
    add_menu_page(
      __('KimaAI', 'kimaai'),
      __('KimaAI', 'kimaai'),
      'manage_options',
      'kimaai',
      [$this, 'adminPage'],
      $mini_logo,
      80
    );

    add_submenu_page(
      'kimaai',
      __('Chatbot Feedback Analytics', 'kimaai'),
      __('Feedback Analytics', 'kimaai'),
      'manage_options',
      'kimaai-feedback-analytics',
      [$this, 'renderFeedbackAnalyticsPage']
    );
  }

  /**
   * The admin page.
   */
  public function adminPage(): void {
    // Before anything else, show the default WordPress admin notices if there are any.
    echo '<div class="wrap">';
    settings_errors();
    echo '</div>';
    echo '<div id="kimaai-settings">';
    echo '</div>';
  }

  /**
   * Renders the feedback analytics page shell.
   */
  public function renderFeedbackAnalyticsPage(): void {
    echo '<div class="wrap"><div id="kimaai-feedback-analytics"></div></div>';
  }

  /**
   * Adds the plugin action links.
   *
   * @hook plugin_action_links_{$plugin_file}
   * @param array $links The plugin action links.
   * @return array The plugin action links.
   */
  public function pluginActionLinks(array $links): array {
    $settings_link = '<a href="admin.php?page=kimaai">' . esc_html__('Settings', 'kimaai') . '</a>';
    array_unshift($links, $settings_link);
    return $links;
  }
}
