<?php

namespace GOSP\GravityOps\Core\SuiteCore;

use GOSP\GravityOps\Core\Utils\AssetHelper;

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

/**
 * Shared admin asset loader for GravityOps screens.
 */
class AdminAssetHelper {

	/**
	 * SuiteCore instance.
	 *
	 * @var SuiteCore
	 */
	private SuiteCore $core;

	/**
	 * Constructor method for initializing the class with a SuiteCore instance.
	 *
	 * @param SuiteCore $core Instance of SuiteCore to be assigned to the class.
	 *
	 * @return void
	 */
	public function __construct( SuiteCore $core ) {
		$this->core = $core;
	}

	/**
	 * Enqueue GravityOps admin assets for the current screen.
	 *
	 * @param bool $is_shell_page True when viewing a GravityOps shell page.
	 * @param bool $is_freemius   True when viewing a Freemius page.
	 * @param bool $is_gf_page    True when viewing a Gravity Forms page.
	 *
	 * @return bool True when assets were enqueued.
	 */
	public function enqueue( bool $is_shell_page, bool $is_freemius, bool $is_gf_page ): bool {
		$load_globally = $this->core->config()->load_admin_assets_globally();

		if ( ! $is_shell_page && ! $is_freemius && ! $is_gf_page && ! $load_globally ) {
			return false;
		}

		$assets  = $this->core->shell()->resolve_assets_urls();
		$version = SuiteCore::VERSION;

		// Cache-bust on the asset's file modification time so library updates surface
		// without requiring a SuiteCore::VERSION bump. Paths are resolved relative to
		// this class so it works for both the symlinked dev copy and built vendor copies.
		$assets_path = dirname( __DIR__, 2 ) . '/assets/';
		$ver         = static function ( string $relative_path ) use ( $assets_path, $version ): string {
			$file = $assets_path . $relative_path;
			return file_exists( $file ) ? AssetHelper::get_file_version( $file ) : $version;
		};

		if ( empty( $assets['css'] ) && empty( $assets['js'] ) && empty( $assets['menu_css'] ) ) {
			return false;
		}

		// Load minimal menu/icon CSS globally or on GF pages.
		if ( ! empty( $assets['menu_css'] ) ) {
			wp_enqueue_style( 'gravityops-menu', $assets['menu_css'], [], $ver( 'css/menu.css' ) );
		}

		// Load the full admin UI CSS only on our shell or Freemius pages.
		if ( ! empty( $assets['css'] ) && ( $is_shell_page || $is_freemius ) ) {
			wp_enqueue_style( 'gravityops-admin', $assets['css'], [ 'gravityops-menu' ], $ver( 'css/admin.css' ) );
		}

		// We only need the JS on our shell pages or Freemius pages where we inject tabs.
		if ( $is_shell_page || $is_freemius ) {
			if ( ! empty( $assets['js'] ) ) {
				wp_enqueue_script( 'gravityops-admin', $assets['js'], [ 'jquery' ], $ver( 'js/admin.js' ), true );
				wp_localize_script(
					'gravityops-admin',
					'gopsAdmin',
					[
						'ajaxUrl'    => admin_url( 'admin-ajax.php' ),
						'nonce'      => wp_create_nonce( 'gops_admin' ),
						'baseUrl'    => admin_url( 'admin.php?page=gravity_ops' ),
						'updatesUrl' => add_query_arg( 'gops_filter', 'updates', admin_url( 'admin.php?page=gravity_ops' ) ),
					]
				);
			}
			// On Freemius pages also load a small override stylesheet to hide native tabs safely.
			if ( $is_freemius && ! empty( $assets['css'] ) ) {
				$fs_css = trailingslashit( dirname( $assets['css'] ) ) . 'freemius.css';
				wp_enqueue_style( 'gravityops-freemius', $fs_css, [ 'gravityops-admin' ], $ver( 'css/freemius.css' ) );
			}
		}

		return true;
	}
}
