<?php

namespace ModernFields\FieldGroups\Hooks;

use ModernFields\WordPress\Interfaces\Hooks;

defined('ABSPATH') || exit;

/**
 * Registers the mf_field_group post type and its meta fields.
 *
 * @since 1.0.0
 */
class FieldGroupPostType implements Hooks
{
  /**
   * @since 1.0.0
   */
  public function registerHooks(): void
  {
    add_action('init', [$this, 'registerPostType']);
    add_action('init', [$this, 'registerMetaFields']);
  }

  /**
   * @since 1.0.0
   */
  public function registerPostType(): void
  {
    register_post_type(MODERN_FIELDS_CPT_SLUG, [
      'label'        => 'Field Group',
      'public'       => false,
      'show_ui'      => true,
      'supports'     => ['title', 'editor', 'author', 'excerpt', 'custom-fields', 'revisions'],
      'show_in_rest' => true,
      'rest_base'    => MODERN_FIELDS_CPT_REST_BASE,
    ]);
  }

  /**
   * @since 1.0.0
   */
  public function registerMetaFields(): void
  {
    register_post_meta(MODERN_FIELDS_CPT_SLUG, 'mf_location', [
      'type'         => 'string',
      'description'  => __('The location where the field group should appear', 'modern-fields'),
      'single'       => true,
      'show_in_rest' => true,
      'default'      => '',
    ]);

    register_post_meta(MODERN_FIELDS_CPT_SLUG, 'mf_scope', [
      'type'         => 'string',
      'description'  => __('The scope where the field group should appear', 'modern-fields'),
      'single'       => true,
      'show_in_rest' => true,
      'default'      => '',
    ]);

    register_post_meta(MODERN_FIELDS_CPT_SLUG, 'mf_targets', [
      'type'         => 'string',
      'description'  => __('The target where the field group should appear (multiple meta entries, one per target ID)', 'modern-fields'),
      'single'       => false,
      'show_in_rest' => true,
    ]);

    register_post_meta(MODERN_FIELDS_CPT_SLUG, 'mf_roles', [
      'type'         => 'string',
      'description'  => __('The roles that should have access to the field group (multiple meta entries, one per role)', 'modern-fields'),
      'single'       => false,
      'show_in_rest' => true,
    ]);

    register_post_meta(MODERN_FIELDS_CPT_SLUG, 'mf_settings', [
      'single'       => true,
      'type'         => 'object',
      'default'      => [],
      'show_in_rest' => [
        'schema' => [
          'type'       => 'object',
          'properties' => [
            'name'     => ['type' => 'string', 'context' => ['view', 'edit']],
            'icon'     => ['type' => 'string', 'context' => ['view', 'edit']],
            'position' => ['type' => 'number', 'context' => ['view', 'edit']],
            'category' => ['type' => 'string', 'context' => ['view', 'edit']],
          ],
        ],
      ],
    ]);

    register_post_meta(MODERN_FIELDS_CPT_SLUG, 'mf_cpt', [
      'type'         => 'string',
      'description'  => __('The custom post type for the field group', 'modern-fields'),
      'single'       => true,
      'show_in_rest' => true,
      'default'      => '',
    ]);

    register_post_meta(MODERN_FIELDS_CPT_SLUG, 'mf_position', [
      'type'         => 'string',
      'description'  => __('The position of the field group', 'modern-fields'),
      'single'       => true,
      'show_in_rest' => true,
      'default'      => '',
    ]);
  }
}
