<?php
/**
 * PathResolver class.
 *
 * Resolves absolute paths and URLs to plugin and module files for asset loading, view rendering, and structure mapping.
 *
 * @package Ilabs\Firevert\Core\Services\Resolver
 */

declare( strict_types=1 );

namespace Ilabs\Firevert\Core\Services\Resolver;

use Ilabs\Firevert\Core\ModuleManager;

/**
 * Class PathResolver
 *
 * Resolves file and URL paths within the plugin structure.
 *
 * @package Ilabs\Firevert\Core\Services
 */
class PathResolver {

	/**
	 * Base filesystem path of the plugin.
	 *
	 * @var string
	 */
	private string $base_path;

	/**
	 * Base URL of the plugin.
	 *
	 * @var string
	 */
	private string $base_url;

	/**
	 * ModuleManager service
	 *
	 * @var ModuleManager
	 */
	private ModuleManager $module_manager;

	/**
	 * PathResolver constructor.
	 *
	 * @param string        $base_path The base filesystem path of the plugin.
	 * @param string        $base_url The base URL of the plugin.
	 * @param ModuleManager $module_manager ModuleManager instance.
	 */
	public function __construct( string $base_path, string $base_url, ModuleManager $module_manager ) {
		$this->base_path      = rtrim( $base_path, '/' );
		$this->base_url       = rtrim( $base_url, '/' );
		$this->module_manager = $module_manager;
	}

	/**
	 * Get the base filesystem path of the plugin.
	 *
	 * @return string The base path.
	 */
	public function get_base_path(): string {
		return $this->base_path;
	}

	/**
	 * Get the base URL of the plugin.
	 *
	 * @return string The base URL.
	 */
	public function get_base_url(): string {
		return $this->base_url;
	}

	/**
	 * Get the path to the Core directory or a file within it.
	 *
	 * @param string $path Optional path relative to the Core directory.
	 *
	 * @return string The resolved path.
	 */
	public function get_core_path( string $path = '' ): string {
		return $this->resolve_path( 'src/Core', $path );
	}

	/**
	 * Get the path to the Modules directory or a file within it.
	 *
	 * @param string $path Optional path relative to the Modules directory.
	 *
	 * @return string The resolved path.
	 */
	public function get_modules_path( string $path = '' ): string {
		return $this->resolve_path( 'src/Modules', $path );
	}

	/**
	 * Get the path to the public/dist directory or a file within it.
	 *
	 * @param string $path Optional path relative to the public/dist directory.
	 *
	 * @return string The resolved path.
	 */
	public function get_core_dist_path( string $path = '' ): string {
		return $this->resolve_path( 'resources/dist', $path );
	}

	/**
	 * Get the URL to the public/dist directory or a file within it.
	 *
	 * @param string $path Optional path relative to the public/dist directory.
	 *
	 * @return string The resolved URL.
	 */
	public function get_core_dist_url( string $path = '' ): string {
		return $this->resolve_url( 'resources/dist', $path );
	}

	/**
	 * Get the path to a specific module directory or a file within it.
	 *
	 * @param string $module_slug The module slug.
	 * @param string $path Optional path relative to the module directory.
	 *
	 * @return string The resolved path.
	 */
	public function get_module_path( string $module_slug, string $path = '' ): string {
		$folder = $this->module_manager->get_folder_by_slug( $module_slug ) ?? $module_slug;

		return $this->resolve_path( "src/Modules/{$folder}", $path );
	}

	/**
	 * Get the URL to a specific module directory or a file within it.
	 *
	 * @param string $module_slug The module slug.
	 * @param string $path Optional path relative to the module directory.
	 *
	 * @return string The resolved URL.
	 */
	public function get_module_url( string $module_slug, string $path = '' ): string {
		$folder = $this->module_manager->get_folder_by_slug( $module_slug ) ?? $module_slug;

		return $this->resolve_url( "src/Modules/{$folder}", $path );
	}

	/**
	 * Get the path to the resources directory or a file within it.
	 *
	 * @param string $path Optional path relative to the resources directory.
	 *
	 * @return string The resolved path.
	 */
	public function get_resources_path( string $path = '' ): string {
		return $this->resolve_path( 'resources', $path );
	}

	/**
	 * Get the path to a specific module's view file.
	 *
	 * @param string $module_folder The module folder.
	 * @param string $view_path The view file name.
	 *
	 * @return string The resolved path to the view file.
	 */
	public function get_module_view_path( string $module_folder, string $view_path ): string {
		return $this->resolve_path( "src/Modules/{$module_folder}/resources/views", $view_path );
	}

	/**
	 * Resolve a path relative to the base path.
	 *
	 * @param string $base_dir Base directory relative to the plugin root.
	 * @param string $path Path relative to the base directory.
	 *
	 * @return string The resolved filesystem path.
	 */
	private function resolve_path( string $base_dir, string $path ): string {
		return $this->base_path . '/' . trim( $base_dir . '/' . $path, '/' );
	}

	/**
	 * Resolve a URL relative to the base URL.
	 *
	 * @param string $base_dir Base directory relative to the plugin root.
	 * @param string $path Path relative to the base directory.
	 *
	 * @return string The resolved URL.
	 */
	private function resolve_url( string $base_dir, string $path ): string {
		return $this->base_url . '/' . trim( $base_dir . '/' . $path, '/' );
	}
}
