<?php
/**
 * Class Noesia Cs Admin Module.
 *
 * @package Noesia_Commerce_Suite
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Base admin module controller.
 *
 * A module owns tab metadata and delegates output to a presentation view.
 * Business logic remains in services/repositories and is passed through context.
 */
abstract class Noesia_CS_Admin_Module {
	/**
	 * Runtime dependency or state value.
	 *
	 * @var mixed
	 */
	protected $key = '';
	/**
	 * Runtime dependency or state value.
	 *
	 * @var mixed
	 */
	protected $label = '';
	/**
	 * Runtime dependency or state value.
	 *
	 * @var mixed
	 */
	protected $icon = '•';
	/**
	 * Runtime dependency or state value.
	 *
	 * @var mixed
	 */
	protected $description = '';
	/**
	 * Runtime dependency or state value.
	 *
	 * @var mixed
	 */
	protected $view = '';
	/**
	 * Runtime dependency or state value.
	 *
	 * @var mixed
	 */
	protected $primary = false;
	/**
	 * Runtime dependency or state value.
	 *
	 * @var mixed
	 */
	protected $container = null;

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $container Container.
	 */
	public function set_container( $container ) {
		if ( $container instanceof Noesia_CS_Container ) {
			$this->container = $container;
		}
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $key Key.
	 */
	protected function service( $key ) {
		$key = sanitize_key( $key );
		return ( $this->container && $key ) ? $this->container->get( $key ) : null;
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $key Key.
	 * @param mixed $default Default.
	 */
	protected function setting( $key, $default = null ) {
		return class_exists( 'Noesia_CS_Settings' ) ? Noesia_CS_Settings::get( $key, $default ) : $default;
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $context Context.
	 */
	public function prepare_data( $context = array() ) {
		return array();
	}

	/**
	 * Handles the related plugin operation.
	 */
	public function key() {
		return $this->key;
	}

	/**
	 * Handles the related plugin operation.
	 */
	public function label() {
		return $this->label;
	}

	/**
	 * Handles the related plugin operation.
	 */
	public function icon() {
		return $this->icon;
	}

	/**
	 * Handles the related plugin operation.
	 */
	public function description() {
		return $this->description;
	}

	/**
	 * Handles the related plugin operation.
	 */
	public function is_primary() {
		return (bool) $this->primary;
	}

	/**
	 * Handles the related plugin operation.
	 */
	public function view_path() {
		return $this->view ? NOESIA_CS_PATH . ltrim( $this->view, '/' ) : '';
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $context Context.
	 */
	public function render( $context = array() ) {
		$context   = is_array( $context ) ? $context : array();
		$prepared  = $this->prepare_data( $context );
		if ( is_array( $prepared ) && $prepared ) {
			$context = array_merge( $context, $prepared );
		}

		$view = $this->view_path();
		if ( ! $view || ! file_exists( $view ) ) {
			if ( isset( $context['admin'] ) && is_object( $context['admin'] ) && method_exists( $context['admin'], 'empty_panel' ) ) {
				$context['admin']->empty_panel( __( 'Module view unavailable.', 'noesia-commerce-suite' ) );
			}
			return;
		}

		extract( $context, EXTR_SKIP ); // phpcs:ignore WordPress.PHP.DontExtract.extract_extract
		include $view;
	}
}
