<?php
/**
 * Enqueues social login CSS/JS only on login, Woo auth, and shortcode contexts.
 *
 * @package WP_PowerSuite\OAuth\UI
 */

namespace WP_PowerSuite\OAuth\UI;

use WP_PowerSuite\OAuth\SocialShortcodeService;

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

final class SocialLoginStyles {

	private const STYLE_HANDLE = 'wpps-social-login';

	private const SCRIPT_HANDLE = 'wpps-social-login-login';

	private const ADMIN_ACCOUNTS_HANDLE = 'wpps-social-accounts-panel';

	private const PANEL_SCRIPT_HANDLE = 'wpps-social-accounts-panel-js';

	private static bool $login_hooked = false;

	private static bool $woo_hooked = false;

	private static bool $shortcode_hooked = false;

	private static bool $panel_hooked = false;

	public static function registerProvider( string $slug ): void {
		// Reserved for future provider-scoped assets; full stylesheet covers all providers.
		unset( $slug );
	}

	public static function ensureLoginPageStyles(): void {
		if ( self::$login_hooked ) {
			return;
		}
		self::$login_hooked = true;
		add_action( 'login_enqueue_scripts', [ self::class, 'enqueueLoginAssets' ], 5 );
	}

	public static function ensureWooCommerceButtonStyles(): void {
		if ( self::$woo_hooked ) {
			return;
		}
		self::$woo_hooked = true;
		add_action( 'wp_enqueue_scripts', [ self::class, 'maybeEnqueueFrontendAssets' ], 20 );
	}

	public static function ensureShortcodeStyles(): void {
		if ( self::$shortcode_hooked ) {
			return;
		}
		self::$shortcode_hooked = true;
		add_action( 'wp_enqueue_scripts', [ self::class, 'maybeEnqueueShortcodeAssets' ], 20 );
	}

	public static function ensureAccountPanelStyles(): void {
		if ( self::$panel_hooked ) {
			return;
		}
		self::$panel_hooked = true;
		add_action( 'admin_enqueue_scripts', [ self::class, 'maybeEnqueueAdminAccountPanelAssets' ], 20 );
		add_action( 'wp_enqueue_scripts', [ self::class, 'maybeEnqueueWooAccountPanelAssets' ], 20 );
	}

	/** @deprecated 1.2.0 Use ensureAccountPanelStyles() */
	public static function ensureAdminAccountPanelStyles(): void {
		self::ensureAccountPanelStyles();
	}

	public static function maybeEnqueueAdminAccountPanelAssets( string $hook_suffix ): void {
		if ( ! in_array( $hook_suffix, [ 'profile.php', 'user-edit.php' ], true ) ) {
			return;
		}
		self::registerAccountPanelStyle();
		self::registerAccountPanelScript();
		wp_enqueue_style( self::ADMIN_ACCOUNTS_HANDLE );
		wp_enqueue_script( self::PANEL_SCRIPT_HANDLE );
	}

	public static function maybeEnqueueWooAccountPanelAssets(): void {
		if ( is_admin() ) {
			return;
		}
		if ( ! class_exists( 'WooCommerce' ) || ! function_exists( 'is_account_page' ) || ! is_account_page() ) {
			if ( ! SocialShortcodeService::wasUsedThisRequest() && ! SocialAssetContext::pageHasSocialShortcode() ) {
				return;
			}
		}
		self::registerAccountPanelStyle();
		wp_enqueue_style( self::ADMIN_ACCOUNTS_HANDLE );
	}

	public static function enqueueLoginAssets(): void {
		self::registerStyle();
		wp_enqueue_style( self::STYLE_HANDLE );
		self::registerLoginScript();
		wp_enqueue_script( self::SCRIPT_HANDLE );
	}

	public static function maybeEnqueueFrontendAssets(): void {
		if ( ! SocialAssetContext::isWooCommerceAuthContext() && ! SocialAssetContext::pageHasSocialShortcode() ) {
			return;
		}
		self::registerStyle();
		wp_enqueue_style( self::STYLE_HANDLE );
	}

	public static function maybeEnqueueShortcodeAssets(): void {
		if ( ! SocialShortcodeService::wasUsedThisRequest() ) {
			return;
		}
		self::registerStyle();
		wp_enqueue_style( self::STYLE_HANDLE );
	}

	private static function registerAccountPanelScript(): void {
		if ( wp_script_is( self::PANEL_SCRIPT_HANDLE, 'registered' ) ) {
			return;
		}
		$path = self::assetPath( 'social-accounts-panel.js' );
		$ver  = file_exists( $path ) ? (string) filemtime( $path ) : '1.0.0';
		wp_register_script(
			self::PANEL_SCRIPT_HANDLE,
			self::assetUrl( 'social-accounts-panel.js' ),
			[],
			$ver,
			true
		);
	}

	private static function registerAccountPanelStyle(): void {
		if ( wp_style_is( self::ADMIN_ACCOUNTS_HANDLE, 'registered' ) ) {
			return;
		}
		$path = self::assetPath( 'social-accounts-panel.css' );
		$ver  = file_exists( $path ) ? (string) filemtime( $path ) : '1.0.0';
		wp_register_style(
			self::ADMIN_ACCOUNTS_HANDLE,
			self::assetUrl( 'social-accounts-panel.css' ),
			[],
			$ver
		);
	}

	private static function registerStyle(): void {
		if ( wp_style_is( self::STYLE_HANDLE, 'registered' ) ) {
			return;
		}
		$path = self::assetPath( 'social-login.css' );
		$ver  = file_exists( $path ) ? (string) filemtime( $path ) : '1.0.0';
		wp_register_style(
			self::STYLE_HANDLE,
			self::assetUrl( 'social-login.css' ),
			[],
			$ver
		);
	}

	private static function registerLoginScript(): void {
		if ( wp_script_is( self::SCRIPT_HANDLE, 'registered' ) ) {
			return;
		}
		$path = self::assetPath( 'social-login-login.js' );
		$ver  = file_exists( $path ) ? (string) filemtime( $path ) : '1.0.0';
		wp_register_script(
			self::SCRIPT_HANDLE,
			self::assetUrl( 'social-login-login.js' ),
			[],
			$ver,
			true
		);
	}

	private static function assetPath( string $file ): string {
		return WP_POWERSUITE_SRC_DIR . 'OAuth' . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . $file;
	}

	private static function assetUrl( string $file ): string {
		return plugins_url( 'src/OAuth/assets/' . $file, WP_POWERSUITE_PLUGIN_FILE );
	}
}
