<?php

namespace Dokan\TryAura\Common;

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

/**
 * Assets class.
 *
 * @since 1.0.0
 */
class Assets {
	/**
	 * Constructor.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		$this->register_all_scripts();
	}

	/**
	 * Register all scripts and styles
	 *
	 * @since 1.0.0
	 */
	public function register_all_scripts() {
		$styles  = $this->get_styles();
		$scripts = $this->get_scripts();

		$this->register_styles( $styles );
		$this->register_scripts( $scripts );
		$this->localize_scripts();

		do_action( 'tryaura_register_scripts' );
	}

	/**
	 * Localize scripts with necessary data
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function localize_scripts() {
		$config = array(
			'aiProviders'       => array(
				'google' => array(
					'gemini-2.5-flash-image'         => array(
						'label'        => __( 'gemini-2.5-flash-image', 'tryaura' ),
						'identity'     => 'image',
						'inputTypes'   => array( 'text', 'image' ),
						'outputTypes'  => array( 'image' ),
						'supported'    => true,
						'locked'       => false,
						'capabilities' => array(
							'image'  => array(
								'supported' => true,
								'locked'    => false,
							),
							'prompt' => array(
								'supported' => true,
								'locked'    => false,
							),
						),
						'parameters'   => array(
							'aspectRatio'      => array(
								'supported' => true,
								'locked'    => false,
								'default'   => '1:1',
								'values'    => array(
									array(
										'label'  => __( '1:1', 'tryaura' ),
										'value'  => '1:1',
										'locked' => false,
									),
									array(
										'label'  => __( '16:9', 'tryaura' ),
										'value'  => '16:9',
										'locked' => false,
									),
									array(
										'label'  => __( '9:16', 'tryaura' ),
										'value'  => '9:16',
										'locked' => false,
									),
									array(
										'label'  => __( '4:3', 'tryaura' ),
										'value'  => '4:3',
										'locked' => false,
									),
									array(
										'label'  => __( '3:4', 'tryaura' ),
										'value'  => '3:4',
										'locked' => false,
									),
								),
							),
							'personGeneration' => array(
								'supported' => true,
								'locked'    => false,
								'default'   => 'allow_adult',
								'values'    => array(
									array(
										'label'  => __( 'Allow Adult', 'tryaura' ),
										'value'  => 'allow_adult',
										'locked' => false,
									),
									array(
										'label'  => __( 'Disallow', 'tryaura' ),
										'value'  => 'disallow',
										'locked' => false,
									),
								),
							),
							'candidateCount'   => array(
								'supported' => true,
								'locked'    => false,
								'default'   => '1',
								'values'    => array(
									array(
										'label'  => __( '1 image', 'tryaura' ),
										'value'  => '1',
										'locked' => false,
									),
								),
							),
						),
					),
				),
			),
			'defaultProvider'   => 'google',
			'defaultImageModel' => 'gemini-2.5-flash-image',
		);

		wp_localize_script( 'tryaura-ai-models', 'tryAuraAiProviderModels', apply_filters( 'tryaura_ai_models', $config ) );
	}

	/**
	 * Get styles.
	 *
	 * @since 1.0.0
	 *
	 * @return array
	 */
	private function get_styles() {
		$asset_url  = plugin_dir_url( TRYAURA_FILE );
		$asset_path = TRYAURA_DIR . '/';
		$styles     = array();

		$css_path = $asset_path . 'build/admin/dashboard/style-index.css';
		if ( file_exists( $css_path ) ) {
			$styles['tryaura-admin'] = array(
				'src'     => $asset_url . 'build/admin/dashboard/style-index.css',
				'deps'    => array( 'wp-components', 'tryaura-components' ),
				'version' => filemtime( $css_path ),
			);
		}

		$css_path = $asset_path . 'build/admin/enhancer/style-index.css';
		if ( file_exists( $css_path ) ) {
			$styles['tryaura-enhancer'] = array(
				'src'     => $asset_url . 'build/admin/enhancer/style-index.css',
				'deps'    => array(),
				'version' => filemtime( $css_path ),
			);
		}

		$css_path = $asset_path . 'build/style-components.css';
		if ( file_exists( $css_path ) ) {
			$styles['tryaura-components'] = array(
				'src'     => $asset_url . 'build/style-components.css',
				'deps'    => array(),
				'version' => filemtime( $css_path ),
			);
		}

		$css_path = $asset_path . 'build/frontend/tryon/style-index.css';
		if ( file_exists( $css_path ) ) {
			$styles['tryaura-tryon'] = array(
				'src'     => $asset_url . 'build/frontend/tryon/style-index.css',
				'deps'    => array( 'tryaura-components' ),
				'version' => filemtime( $css_path ),
			);
		}

		return apply_filters( 'tryaura_styles', $styles );
	}

	/**
	 * Get scripts.
	 *
	 * @since 1.0.0
	 *
	 * @return array
	 */
	public function get_scripts() {
		$asset_url  = plugin_dir_url( TRYAURA_FILE );
		$asset_path = TRYAURA_DIR . '/';
		$scripts    = array();

		$asset_file = $asset_path . 'build/data/settings.asset.php';
		if ( file_exists( $asset_file ) ) {
			$asset   = include $asset_file; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable
			$deps    = $asset['dependencies'] ?? array( 'wp-data' );
			$version = $asset['version'] ?? '1.0.0';

			$scripts['tryaura-settings'] = array(
				'version' => $version,
				'src'     => $asset_url . 'build/data/settings.js',
				'deps'    => $deps,
			);
		}

		$asset_file = $asset_path . 'build/admin/dashboard/index.asset.php';
		if ( file_exists( $asset_file ) ) {
			$asset   = include $asset_file; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable
			$deps    = $asset['dependencies'] ?? array( 'wp-element', 'wp-components', 'wp-api-fetch', 'wp-i18n', 'wp-api', 'tryaura-settings' );
			$version = $asset['version'] ?? '1.0.0';

			$scripts['tryaura-admin'] = array(
				'version' => $version,
				'src'     => $asset_url . 'build/admin/dashboard/index.js',
				'deps'    => $deps,
			);
		}

		$asset_file = $asset_path . 'build/components.asset.php';
		if ( file_exists( $asset_file ) ) {
			$asset   = include $asset_file; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable
			$deps    = $asset['dependencies'] ?? array( 'wp-element' );
			$version = $asset['version'] ?? '1.0.0';

			$scripts['tryaura-components'] = array(
				'version' => $version,
				'src'     => $asset_url . 'build/components.js',
				'deps'    => $deps,
			);
		}

		$asset_file = $asset_path . 'build/admin/enhancer/index.asset.php';
		if ( file_exists( $asset_file ) ) {
			$asset = include $asset_file; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable
			$deps  = $asset['dependencies'] ?? $deps;
			// Ensure media-views is present so wp.media is available.
			if ( ! in_array( 'media-views', $deps, true ) ) {
				$deps[] = 'media-views';
			}
			$version = $asset['version'] ?? '1.0.0';

			$scripts['tryaura-enhancer'] = array(
				'version' => $version,
				'src'     => $asset_url . 'build/admin/enhancer/index.js',
				'deps'    => $deps,
			);
		}

		$asset_file = $asset_path . 'build/data/ai-models.asset.php';
		if ( file_exists( $asset_file ) ) {
			$asset   = include $asset_file; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable
			$deps    = $asset['dependencies'] ?? array( 'wp-data' );
			$version = $asset['version'] ?? '1.0.0';

			$scripts['tryaura-ai-models'] = array(
				'version' => $version,
				'src'     => $asset_url . 'build/data/ai-models.js',
				'deps'    => $deps,
			);
		}

		$asset_file = $asset_path . 'build/frontend/tryon/index.asset.php';
		if ( file_exists( $asset_file ) ) {
			$asset   = include $asset_file; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable
			$deps    = array_merge( $asset['dependencies'] ?? array(), array( 'wp-element' ) );
			$deps    = array_merge( $deps, array( 'wp-data', 'wp-core-data', 'wp-api-fetch' ) );
			$version = $asset['version'] ?? '1.0.0';

			$scripts['tryaura-tryon'] = array(
				'version' => $version,
				'src'     => $asset_url . 'build/frontend/tryon/index.js',
				'deps'    => $deps,
			);
		}

		return apply_filters( 'tryaura_scripts', $scripts );
	}

	/**
	 * Register styles
	 *
	 * @since 1.0.0
	 *
	 * @param array $styles Assets to register.
	 *
	 * @return void
	 */
	public function register_styles( $styles ) {
		foreach ( $styles as $handle => $style ) {
			$deps    = isset( $style['deps'] ) ? $style['deps'] : false;
			$version = isset( $style['version'] ) ? $style['version'] : TRYAURA_PLUGIN_VERSION;

			wp_register_style( $handle, $style['src'], $deps, $version );
		}
	}

	/**
	 * Register scripts
	 *
	 * @since 1.0.0
	 *
	 * @param array $scripts Assets to register.
	 *
	 * @return void
	 */
	public function register_scripts( $scripts ) {
		foreach ( $scripts as $handle => $script ) {
			$deps      = isset( $script['deps'] ) ? $script['deps'] : false;
			$in_footer = isset( $script['in_footer'] ) ? $script['in_footer'] : true;
			$version   = isset( $script['version'] ) ? $script['version'] : TRYAURA_PLUGIN_VERSION;

			wp_register_script( $handle, $script['src'], $deps, $version, $in_footer );
			wp_set_script_translations( $handle, 'tryaura', plugin_dir_path( TRYAURA_FILE ) . 'languages' );
		}
	}

	/**
	 * Enqueue the scripts
	 *
	 * @since 1.0.0
	 *
	 * @param array $scripts Assets to enqueue.
	 *
	 * @return void
	 */
	public function enqueue_scripts( $scripts ) {
		foreach ( $scripts as $handle => $script ) {
			wp_enqueue_script( $handle );
		}
	}
}
