<?php
/**
 * Base Module
 *
 * Abstract base class for all modules
 *
 * @package NemtlyBooking
 * @since   PBOOKING_SINCE
 */

namespace NemtlyBooking\Core;

use NemtlyBooking\Interfaces\Module_Interface;

/**
 * Base Module class
 *
 * @since PBOOKING_SINCE
 */
abstract class Module implements Module_Interface {

    /**
     * Container instance
     *
     * @var Container
     */
    protected $container;

    /**
     * Module name
     *
     * @var string
     */
    protected $name;

    /**
     * Module dependencies
     *
     * @var array
     */
    protected $dependencies = [];

    /**
     * Whether module is enabled
     *
     * @var bool
     */
    protected $enabled = true;

    /**
     * Constructor
     *
     * @since PBOOKING_SINCE
     *
     * @param Container $container The dependency injection container.
     */
    public function __construct( Container $container ) {
        $this->container = $container;
    }

    /**
     * Initialize the module
     *
     * @since PBOOKING_SINCE
     */
    abstract public function init();

    /**
     * Get module name
     *
     * @since PBOOKING_SINCE
     *
     * @return string Module name.
     */
    public function get_name() {
        return $this->name;
    }

    /**
     * Get module dependencies
     *
     * @since PBOOKING_SINCE
     *
     * @return array Array of module names this module depends on.
     */
    public function get_dependencies() {
        return $this->dependencies;
    }

    /**
     * Check if module is enabled
     *
     * @since PBOOKING_SINCE
     *
     * @return bool True if enabled, false otherwise.
     */
    public function is_enabled() {
        return $this->enabled;
    }

    /**
     * Set module enabled status
     *
     * @since PBOOKING_SINCE
     *
     * @param bool $enabled Whether module is enabled.
     */
    public function set_enabled( $enabled ) {
        $this->enabled = (bool) $enabled;
    }

    /**
     * Get container instance
     *
     * @since PBOOKING_SINCE
     *
     * @return Container
     */
    protected function get_container() {
        return $this->container;
    }

    /**
     * Get a service from the container
     *
     * @since PBOOKING_SINCE
     *
     * @param string $service_name Service name.
     *
     * @return mixed Service instance.
     */
    protected function get_service( $service_name ) {
        return $this->container->get( $service_name );
    }

    /**
     * Register module hooks
     *
     * @since PBOOKING_SINCE
     *
     * @param string   $hook_name Hook name.
     * @param callable $callback  Callback function.
     * @param int      $priority  Hook priority.
     * @param int      $accepted_args Number of accepted arguments.
     */
    protected function add_hook( $hook_name, $callback, $priority = 10, $accepted_args = 1 ) {
        add_action( $hook_name, $callback, $priority, $accepted_args );
    }

    /**
     * Register module filter
     *
     * @since PBOOKING_SINCE
     *
     * @param string   $filter_name Filter name.
     * @param callable $callback    Callback function.
     * @param int      $priority    Filter priority.
     * @param int      $accepted_args Number of accepted arguments.
     */
    protected function add_filter( $filter_name, $callback, $priority = 10, $accepted_args = 1 ) {
        add_filter( $filter_name, $callback, $priority, $accepted_args );
    }
}
