<?php

namespace ModernFields\FieldGroups\Hooks;

use ModernFields\FieldGroups\Repositories\FieldGroupRepository;
use ModernFields\FieldGroups\Locations\UseCases\RegisterFieldsUseCase;
use ModernFields\WordPress\Interfaces\Hooks;

defined('ABSPATH') || exit;

/**
 * Registers field meta for all post types, users, and terms via the REST API.
 *
 * Reads field definitions from all published field groups and registers the
 * corresponding meta keys so they are accessible through WordPress REST API routes.
 *
 * @since 1.0.0
 */
class FieldGroupMetas implements Hooks
{
  /**
   * @since 1.0.0
   */
  public function registerHooks(): void
  {
    # Priority 20 so block types are registered first (Blocks hook runs at 10)
    add_action('init', [$this, 'registerMetaFields'], 20);
    add_action('rest_api_init', [$this, 'registerMetaFields']);
  }

  /**
   * @since 1.0.0
   */
  public function registerMetaFields(): void
  {
    $field_groups = FieldGroupRepository::getAllFieldGroups();

    $registerFieldsUseCase = new RegisterFieldsUseCase($field_groups);
    $registerFieldsUseCase->execute();
  }
}
