<?php

namespace ModernFields\FieldGroups\Locations\Hooks;

use ModernFields\FieldGroups\Entities\FieldGroup;
use ModernFields\FieldGroups\Locations\Location;
use ModernFields\FieldGroups\Locations\UseCases\GroupAssetsLoaderUseCase;
use ModernFields\FieldGroups\Repositories\FieldGroupRepository;
use ModernFields\FieldGroups\Services\OptionsService;
use ModernFields\WordPress\Security\SecurityHelper;
use ModernFields\WordPress\Interfaces\Hooks;

defined('ABSPATH') || exit;

/**
 * DashboardLocation Class
 *
 * Renders field groups as widgets on the WordPress admin Dashboard.
 * Each group targeting the 'dashboard' location becomes its own widget,
 * mounted as a React app that saves its values as site options.
 *
 * Everything is gated on manage_options: values go through /wp/v2/settings,
 * which WordPress restricts to that capability — the widget is never shown
 * to users who could not save it.
 *
 * @since 1.3.0
 */
class DashboardLocation extends Location implements Hooks
{
  /**
   * Register the WordPress hooks required for the dashboard location.
   *
   * @since 1.3.0
   * @return void
   */
  public function registerHooks(): void
  {
    add_action('wp_dashboard_setup', [$this, 'registerWidgets']);
    add_action('admin_enqueue_scripts', [$this, 'enqueueAssets']);
  }

  /**
   * Checks if the current admin screen is the Dashboard.
   *
   * @since 1.3.0
   * @return bool True if the current screen is the Dashboard, false otherwise.
   */
  protected function isDashboardPage(): bool
  {
    return SecurityHelper::isCurrentAdminPage(['index.php']);
  }

  /**
   * Register one dashboard widget per field group targeting the dashboard.
   *
   * @since 1.3.0
   * @return void
   */
  public function registerWidgets(): void
  {
    if (!current_user_can('manage_options')) {
      return;
    }

    $field_groups = FieldGroupRepository::getDashboardFieldGroups();

    foreach ($field_groups as $field_group) {
      wp_add_dashboard_widget(
        'modern-fields-' . $field_group->getId(),
        $field_group->getTitle(),
        function () use ($field_group) {
          echo wp_kses_post($this->renderMarkup($field_group));
        }
      );
    }
  }

  /**
   * Render the React root container for the given field group.
   *
   * @since 1.3.0
   * @param FieldGroup $field_group The field group to render the markup for.
   * @return string The rendered markup.
   */
  protected function renderMarkup(FieldGroup $field_group): string
  {
    return '<div class="mf-dashboard-fields-root" data-group-id="' . esc_attr($field_group->getId()) . '"></div>';
  }

  /**
   * Enqueue assets for the dashboard widgets React application.
   *
   * @since 1.3.0
   * @return void
   */
  public function enqueueAssets(): void
  {
    if (!$this->isDashboardPage() || !current_user_can('manage_options')) {
      return;
    }

    $field_groups = FieldGroupRepository::getDashboardFieldGroups();

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

    $context = ['options' => OptionsService::getRegisteredOptions()];

    $groupAssetsLoaderUseCase = new GroupAssetsLoaderUseCase('dashboard-fields', $field_groups, $context);
    $groupAssetsLoaderUseCase->execute();
  }
}
