<?php

namespace Fotogenic_AI;

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

final class Modules_Registry {
    private static ?Modules_Registry $instance = null;
    private function __construct() {}

    public static function instance(): Modules_Registry {
        if ( null === self::$instance ) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    public function init(): void {
        $this->register_modules();
        add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
    }

    private function register_modules(): void {
        // Studio admin integrations are registered here.
        $modules = [
            Studio_Module::instance(),
        ];
        foreach ( $modules as $module ) {
            $module->init();
        }
    }

    public function enqueue_assets( string $hook ): void {
        if ( ! fotogenic_ai_user_can_access() ) {
            return;
        }
        $settings_hook = Settings_Page::instance()->get_settings_page_hook();
        $is_settings = $settings_hook && $hook === $settings_hook;
        $is_product_screen = Admin::is_classic_product_editor_screen( $hook );

        if ( ! $is_settings && ! $is_product_screen ) {
            return;
        }

		if ( $is_settings ) {
			$this->enqueue_shared_assets();
			Assets_Manifest::enqueue_style( 'fotogenic-ai-settings' );
            Assets_Manifest::enqueue_script( 'fotogenic-ai-settings' );
	            wp_add_inline_script(
	                'fotogenic-ai-settings',
	                fotogenic_ai_build_inline_json_assignment(
	                    [ 'FotogenicAI', 'Admin', 'settings' ],
	                    [
	                        'restUrl'             => rtrim( rest_url( 'fotogenic-ai/v1' ), '/' ),
	                        'restNonce'           => wp_create_nonce( 'wp_rest' ),
	                        'rest'                => [
	                            'experienceClaim'   => '/experiences/claim',
	                            'experienceDismiss' => '/experiences/dismiss',
	                        ],
	                        'experienceImages'    => Assets_Manifest::studio_experience_images(),
	                        'auth'                => Auth_Session::instance()->get_public_status(),
	                        'authNotice'          => Auth_Session::instance()->consume_notice_for_current_user(),
	                        'debugLoggingEnabled' => ! empty( Settings::instance()->get_options()['debug_logging'] ),
	                    ]
	                ),
	                'before'
	            );
            wp_set_script_translations(
                'fotogenic-ai-settings',
                'fotogenic-ai',
                FOTOGENIC_AI_PATH . 'languages'
            );
        }
    }

	public function enqueue_shared_assets(): void {
		Assets_Manifest::enqueue_style( 'fotogenic-ai-shared-ui-icons-css' );
		Assets_Manifest::enqueue_style( 'fotogenic-ai-direct-port-css' );
		Assets_Manifest::enqueue_style( 'fotogenic-ai-shared' );
		Assets_Manifest::enqueue_script( 'fotogenic-ai-shared-namespace' );
		Assets_Manifest::enqueue_script( 'fotogenic-ai-shared-ui-icon' );
		Assets_Manifest::enqueue_script( 'fotogenic-ai-shared-rest-fetch' );
		Assets_Manifest::enqueue_script( 'fotogenic-ai-direct-port' );
        Assets_Manifest::enqueue_script( 'fotogenic-ai-shared-core-credits-cache' );
		wp_set_script_translations( 'fotogenic-ai-direct-port', 'fotogenic-ai', FOTOGENIC_AI_PATH . 'languages' );
        wp_add_inline_script(
            'fotogenic-ai-shared-rest-fetch',
            fotogenic_ai_build_inline_json_assignment(
                [ 'FotogenicAI', 'Shared', 'i18n', 'restFetch' ],
                [
                    'restContextMissing' => __( 'REST context is missing.', 'fotogenic-ai' ),
                    'unexpectedResponse' => __( 'Unexpected response from server.', 'fotogenic-ai' ),
                    'requestTimedOut'    => __( 'Request timed out.', 'fotogenic-ai' ),
                    'networkError'       => __( 'Network error.', 'fotogenic-ai' ),
                ]
            ),
            'before'
        );
    }
}
