<?php
/**
 * Hook and Filter Loader
 *
 * @package PackshotPhotoAI
 */

namespace PackshotPhotoAI;

defined('ABSPATH') || exit;

/**
 * Register and manage WordPress hooks
 */
class Loader {

    /**
     * Registered actions
     *
     * @var array
     */
    private array $actions = [];

    /**
     * Registered filters
     *
     * @var array
     */
    private array $filters = [];

    /**
     * Add action
     *
     * @param string $hook          Hook name
     * @param object $component     Component object
     * @param string $callback      Method name
     * @param int    $priority      Priority
     * @param int    $accepted_args Accepted arguments count
     */
    public function add_action(string $hook, object $component, string $callback, int $priority = 10, int $accepted_args = 1): void {
        $this->actions[] = [
            'hook'          => $hook,
            'component'     => $component,
            'callback'      => $callback,
            'priority'      => $priority,
            'accepted_args' => $accepted_args,
        ];
    }

    /**
     * Add filter
     *
     * @param string $hook          Hook name
     * @param object $component     Component object
     * @param string $callback      Method name
     * @param int    $priority      Priority
     * @param int    $accepted_args Accepted arguments count
     */
    public function add_filter(string $hook, object $component, string $callback, int $priority = 10, int $accepted_args = 1): void {
        $this->filters[] = [
            'hook'          => $hook,
            'component'     => $component,
            'callback'      => $callback,
            'priority'      => $priority,
            'accepted_args' => $accepted_args,
        ];
    }

    /**
     * Register all hooks with WordPress
     */
    public function run(): void {
        foreach ($this->actions as $hook) {
            add_action(
                $hook['hook'],
                [$hook['component'], $hook['callback']],
                $hook['priority'],
                $hook['accepted_args']
            );
        }

        foreach ($this->filters as $hook) {
            add_filter(
                $hook['hook'],
                [$hook['component'], $hook['callback']],
                $hook['priority'],
                $hook['accepted_args']
            );
        }
    }
}
