<?php
/**
 * BookiFlex Core
 *
 * @package    BookiFlex
 * @subpackage Core
 * @author     BookiFlex
 * @copyright  Copyright (c) 2025 BookiFlex
 * @license    GPL-2.0-or-later
 *
 * @link       https://bookiflex.com
 */

declare(strict_types=1);

namespace BookiFlex\Core\Trait;

defined( 'ABSPATH' ) || exit;

/**
 * WordPress hooks management trait
 */
trait HooksTrait
{
    /**
     * Registered hooks storage
     */
    private array $_hooks = [];

    /**
     * Add action hook
     */
    public function addAction(string $hook, callable $callback, int $priority = 10, int $acceptedArgs = 1): void
    {
        add_action($hook, $callback, $priority, $acceptedArgs);

        $this->_hooks['actions'][] = [
            'hook' => $hook,
            'callback' => $callback,
            'priority' => $priority,
            'accepted_args' => $acceptedArgs,
        ];
    }

    /**
     * Add filter hook
     */
    public function addFilter(string $hook, callable $callback, int $priority = 10, int $acceptedArgs = 1): void
    {
        add_filter($hook, $callback, $priority, $acceptedArgs);

        $this->_hooks['filters'][] = [
            'hook' => $hook,
            'callback' => $callback,
            'priority' => $priority,
            'accepted_args' => $acceptedArgs,
        ];
    }

    /**
     * Remove all registered hooks
     */
    public function clearHooks(): void
    {
        if (isset($this->_hooks['actions'])) {
            foreach ($this->_hooks['actions'] as $action) {
                remove_action($action['hook'], $action['callback'], $action['priority']);
            }
        }

        if (isset($this->_hooks['filters'])) {
            foreach ($this->_hooks['filters'] as $filter) {
                remove_filter($filter['hook'], $filter['callback'], $filter['priority']);
            }
        }

        $this->_hooks = [];
    }

    /**
     * Get all registered hooks
     */
    public function getRegisteredHooks(): array
    {
        return $this->_hooks;
    }
}
