<?php

namespace Fotogenic_AI;

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

/**
 * Central manifest of script and style asset metadata (handle => path, deps, ver, in_footer/media).
 * Used to enqueue assets without duplicating paths and dependency arrays across Modules_Registry,
 * Studio_Module, and Studio_Page. No file moves, no handle renames, no behavior change.
 */
final class Assets_Manifest {

	/**
	 * Approved Studio artwork keyed by the server-owned experience kind.
	 *
	 * Settings intentionally keeps the existing collage inventory above while
	 * Studio uses the canonical single-image grant presentation.
	 *
	 * @return array<string, string>
	 */
	public static function studio_experience_images(): array {
		$base_url = defined( 'FOTOGENIC_AI_URL' ) ? FOTOGENIC_AI_URL : '';

		return [
			'welcome_connected' => $base_url . 'assets/shared/img/experiences/welcome-model.jpg',
		];
	}

	/**
	 * Script entries: handle => [ path (relative to plugin), deps, in_footer, ver ].
	 *
	 * @return array<string, array{path: string, deps: string[], in_footer: bool, ver?: string}>
	 */
	public static function scripts(): array {
		$v = defined( 'FOTOGENIC_AI_VERSION' ) ? FOTOGENIC_AI_VERSION : '1.0.8';
		return [
			// Shared (Modules_Registry::enqueue_shared_assets)
			'fotogenic-ai-shared-namespace' => [
				'path'       => 'assets/shared/core/namespace.js',
				'deps'       => [],
				'in_footer'  => true,
				'ver'        => $v,
			],
			'fotogenic-ai-shared-ui-icon' => [
				'path'       => 'assets/shared/ui/icon.js',
				'deps'       => [ 'fotogenic-ai-shared-namespace' ],
				'in_footer'  => true,
				'ver'        => $v,
			],
			'fotogenic-ai-shared-rest-fetch' => [
				'path'       => 'assets/shared/api/rest-fetch.js',
				'deps'       => [ 'fotogenic-ai-shared-namespace' ],
				'in_footer'  => true,
				'ver'        => $v,
			],
			'fotogenic-ai-direct-port' => [
				'path'       => 'assets/generated/fotogenic-ui.js',
				'deps'       => [ 'fotogenic-ai-shared-namespace', 'fotogenic-ai-shared-rest-fetch', 'fotogenic-ai-shared-ui-icon', 'fotogenic-ai-shared-core-credits-cache', 'wp-i18n' ],
				'in_footer'  => true,
				'ver'        => $v,
			],
			'fotogenic-ai-shared-core-credits-cache' => [
				'path'       => 'assets/shared/core/credits/credits-cache.js',
				'deps'       => [ 'fotogenic-ai-shared-namespace' ],
				'in_footer'  => true,
				'ver'        => $v,
			],
			// Settings (Modules_Registry::enqueue_assets)
			'fotogenic-ai-settings' => [
				'path'       => 'assets/settings/settings.js',
				'deps'       => [ 'wp-i18n', 'fotogenic-ai-shared-rest-fetch', 'fotogenic-ai-direct-port', 'fotogenic-ai-shared-core-credits-cache', 'fotogenic-ai-shared-ui-icon' ],
				'in_footer'  => true,
				'ver'        => $v,
			],
			// Universal Studio Compose and Review runtime.
			'fotogenic-ai-studio-store' => [
				'path'       => 'assets/studio/store.js',
				'deps'       => [ 'wp-i18n' ],
				'in_footer'  => true,
				'ver'        => $v,
			],
			'fotogenic-ai-studio-transport' => [
				'path'       => 'assets/studio/transport.js',
				'deps'       => [ 'wp-api-fetch', 'fotogenic-ai-studio-store' ],
				'in_footer'  => true,
				'ver'        => $v,
			],
			'fotogenic-ai-studio-source-picker' => [
				'path'       => 'assets/studio/ui/source-picker.js',
				'deps'       => [ 'media-views', 'fotogenic-ai-studio-view-model', 'fotogenic-ai-shared-ui-icon', 'fotogenic-ai-direct-port' ],
				'in_footer'  => true,
				'ver'        => $v,
			],
			'fotogenic-ai-studio-recipe-form' => [
				'path'       => 'assets/studio/ui/recipe-form.js',
				'deps'       => [ 'media-views', 'fotogenic-ai-studio-view-model', 'fotogenic-ai-shared-ui-icon' ],
				'in_footer'  => true,
				'ver'        => $v,
			],
			'fotogenic-ai-studio-summary' => [
				'path'       => 'assets/studio/ui/summary.js',
				'deps'       => [ 'fotogenic-ai-studio-view-model', 'fotogenic-ai-shared-ui-icon' ],
				'in_footer'  => true,
				'ver'        => $v,
			],
			'fotogenic-ai-studio-shell' => [
				'path'       => 'assets/studio/ui/shell.js',
				'deps'       => [ 'fotogenic-ai-shared-ui-icon', 'fotogenic-ai-direct-port', 'fotogenic-ai-studio-store', 'fotogenic-ai-studio-summary' ],
				'in_footer'  => true,
				'ver'        => $v,
			],
			'fotogenic-ai-studio-view-model' => [
				'path'       => 'assets/studio/policies/view-model.js',
				'deps'       => [ 'fotogenic-ai-studio-store' ],
				'in_footer'  => true,
				'ver'        => $v,
			],
			'fotogenic-ai-studio-progress' => [
				'path'       => 'assets/studio/ui/progress.js',
				'deps'       => [ 'fotogenic-ai-studio-view-model', 'fotogenic-ai-shared-ui-icon' ],
				'in_footer'  => true,
				'ver'        => $v,
			],
			'fotogenic-ai-studio-bulk-view' => [
				'path'       => 'assets/studio/ui/bulk.js',
				'deps'       => [ 'fotogenic-ai-studio-view-model', 'fotogenic-ai-shared-ui-icon' ],
				'in_footer'  => true,
				'ver'        => $v,
			],
			'fotogenic-ai-studio-results' => [
				'path'       => 'assets/studio/ui/results.js',
				'deps'       => [ 'media-views', 'fotogenic-ai-studio-view-model', 'fotogenic-ai-shared-ui-icon', 'fotogenic-ai-direct-port' ],
				'in_footer'  => true,
				'ver'        => $v,
			],
			'fotogenic-ai-studio-history' => [
				'path'       => 'assets/studio/ui/history.js',
				'deps'       => [ 'fotogenic-ai-studio-view-model', 'fotogenic-ai-shared-ui-icon' ],
				'in_footer'  => true,
				'ver'        => $v,
			],
			'fotogenic-ai-studio-run-controller' => [
				'path'       => 'assets/studio/run-controller.js',
				'deps'       => [
					'fotogenic-ai-studio-store',
					'fotogenic-ai-studio-transport',
					'fotogenic-ai-studio-progress',
					'fotogenic-ai-studio-bulk-view',
					'fotogenic-ai-studio-results',
					'fotogenic-ai-studio-history',
				],
				'in_footer'  => true,
				'ver'        => $v,
			],
			'fotogenic-ai-studio-app' => [
				'path'       => 'assets/studio/app.js',
				'deps'       => [
					'fotogenic-ai-studio-store',
					'fotogenic-ai-studio-transport',
					'fotogenic-ai-studio-source-picker',
					'fotogenic-ai-studio-recipe-form',
					'fotogenic-ai-studio-summary',
					'fotogenic-ai-studio-shell',
					'fotogenic-ai-studio-run-controller',
				],
				'in_footer'  => true,
				'ver'        => $v,
			],
			// Studio (Studio_Page::enqueue_assets).
			'fotogenic-ai-studio-bootstrap' => [
				'path'       => 'assets/studio/bootstrap.js',
				'deps'       => [ 'fotogenic-ai-studio-app' ],
				'in_footer'  => true,
				'ver'        => $v,
			],
		];
	}

	/**
	 * Universal Studio script handles, including modules with their own JS catalogs.
	 *
	 * @return string[]
	 */
	public static function studio_script_handles(): array {
		return array_values(
			array_filter(
				array_keys( self::scripts() ),
				static fn( string $handle ): bool => 0 === strpos( $handle, 'fotogenic-ai-studio-' )
			)
		);
	}

	/**
	 * Style entries: handle => [ path (relative to plugin), deps, media (optional), ver ].
	 *
	 * @return array<string, array{path: string, deps: string[], media?: string, ver?: string}>
	 */
	public static function styles(): array {
		$ver = defined( 'FOTOGENIC_AI_VERSION' ) ? FOTOGENIC_AI_VERSION : '1.0.8';
		return [
			'fotogenic-ai-shared-ui-icons-css' => [
				'path'  => 'assets/shared/ui/icons.css',
				'deps'  => [],
				'ver'   => '2.1.2',
			],
			// Shared (Modules_Registry::enqueue_shared_assets)
			'fotogenic-ai-shared' => [
				'path'  => 'assets/shared/shared.css',
				'deps'  => [ 'fotogenic-ai-shared-ui-icons-css' ],
				'ver'   => $ver,
			],
			// Settings (Modules_Registry::enqueue_assets)
			'fotogenic-ai-settings' => [
				'path'  => 'assets/settings/settings.css',
				'deps'  => [ 'fotogenic-ai-shared', 'fotogenic-ai-direct-port-css' ],
				'ver'   => $ver,
			],
			'fotogenic-ai-direct-port-css' => [
				'path'  => 'assets/generated/fotogenic-ui.css',
				'deps'  => [ 'fotogenic-ai-shared-ui-icons-css' ],
				'ver'   => $ver,
			],
			// Studio (Studio_Page::enqueue_assets)
			'fotogenic-ai-studio' => [
				'path'  => 'assets/studio/studio.css',
				'deps'  => [ 'fotogenic-ai-shared', 'fotogenic-ai-direct-port-css' ],
				'ver'   => $ver,
			],
		];
	}

	/**
	 * Enqueue a script by handle using manifest metadata.
	 *
	 * @param string $handle Script handle (must exist in scripts()).
	 */
	public static function enqueue_script( string $handle ): void {
		$scripts = self::scripts();
		if ( ! isset( $scripts[ $handle ] ) ) {
			return;
		}
		$entry = $scripts[ $handle ];
		$url = ( defined( 'FOTOGENIC_AI_URL' ) ? FOTOGENIC_AI_URL : '' ) . $entry['path'];
		$ver = $entry['ver'] ?? ( defined( 'FOTOGENIC_AI_VERSION' ) ? FOTOGENIC_AI_VERSION : '1.0.8' );
		$in_footer = $entry['in_footer'] ?? true;
		wp_enqueue_script( $handle, $url, $entry['deps'], $ver, $in_footer );
	}

	/**
	 * Enqueue a style by handle using manifest metadata.
	 *
	 * @param string $handle Style handle (must exist in styles()).
	 */
	public static function enqueue_style( string $handle ): void {
		$styles = self::styles();
		if ( ! isset( $styles[ $handle ] ) ) {
			return;
		}
		$entry = $styles[ $handle ];
		$url = ( defined( 'FOTOGENIC_AI_URL' ) ? FOTOGENIC_AI_URL : '' ) . $entry['path'];
		$ver = $entry['ver'] ?? ( defined( 'FOTOGENIC_AI_VERSION' ) ? FOTOGENIC_AI_VERSION : '1.0.8' );
		$media = $entry['media'] ?? 'all';
		wp_enqueue_style( $handle, $url, $entry['deps'], $ver, $media );
	}
}
