<?php
declare(strict_types=1);
namespace ConfManager\Frontend;

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


use ConfManager\Traits\Singleton;
use ConfManager\Traits\HasHooks;

/**
 * フロント画面のショートコード登録とスクリプトエンキュー
 *
 * @since 1.0.0
 */
class PublicController {

	use Singleton;
	use HasHooks;

	/**
	 * 初期化
	 */
	protected function init(): void {
		$this->register_hooks();
	}

	/**
	 * フック登録
	 */
	protected function register_hooks(): void {
		$this->add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
		$this->add_action( 'init', [ $this, 'register_shortcodes' ] );
	}

	/**
	 * ショートコードを登録
	 */
	public function register_shortcodes(): void {
		add_shortcode( 'conf_manager', [ $this, 'render_shortcode' ] );
	}

	/**
	 * ショートコード出力
	 *
	 * @param array $atts ショートコード属性
	 * @return string HTML出力
	 */
	public function render_shortcode( array $atts ): string {
		$atts = shortcode_atts(
			[ 'view' => 'registration' ],
			$atts,
			'conf_manager'
		);

		// Core でサポートする view のみ許可（不明な view は registration にフォールバック）
		$supported_views = [ 'registration', 'mypage' ];
		if ( ! in_array( $atts['view'], $supported_views, true ) ) {
			$atts['view'] = 'registration';
		}

		wp_enqueue_script( 'conf-manager-public' );
		wp_enqueue_style( 'conf-manager-public' );

		return '<div id="conf-manager-public-app" data-view="' . esc_attr( $atts['view'] ) . '"></div>';
	}

	/**
	 * フロント画面用スクリプト・スタイルを登録
	 */
	public function enqueue_scripts(): void {
		$asset_file = CONF_MANAGER_PLUGIN_DIR . 'build/public-app.asset.php';
		$asset      = file_exists( $asset_file )
			? require $asset_file
			: [ 'dependencies' => [], 'version' => CONF_MANAGER_VERSION ];

		wp_register_script(
			'conf-manager-public',
			CONF_MANAGER_PLUGIN_URL . 'build/public-app.js',
			$asset['dependencies'],
			$asset['version'],
			true
		);

		wp_set_script_translations(
			'conf-manager-public',
			'conferencemanager',
			CONF_MANAGER_PLUGIN_DIR . 'languages'
		);

		// 決済設定をフロントに渡す
		$payment_methods = get_option( 'conf_manager_payment_methods', [ 'bank' ] );
		if ( ! is_array( $payment_methods ) ) {
			$payment_methods = [ 'bank' ];
		}

		$stripe_enabled = \ConfManager\Payment\StripePayment::instance()->is_configured()
			&& in_array( 'stripe', $payment_methods, true );

		wp_localize_script( 'conf-manager-public', 'confManagerPayment', [
			'methods'              => $payment_methods,
			'stripeEnabled'        => $stripe_enabled,
			'stripePublishableKey' => $stripe_enabled
				? \ConfManager\Payment\StripePayment::instance()->get_publishable_key_for_frontend()
				: '',
		] );

		// 利用規約ページURL
		$terms_page = get_page_by_path( 'c-terms' );
		$terms_url  = $terms_page ? get_permalink( $terms_page ) : '';
		wp_localize_script( 'conf-manager-public', 'confManagerTerms', [
			'url' => $terms_url,
		] );

		// セキュリティ: CSRF トークン + ボットガードチャレンジ
		$bot_challenge = \ConfManager\Security\BotGuard::instance()->generate_challenge();
		wp_localize_script( 'conf-manager-public', 'confManagerSecurity', [
			'csrfToken' => wp_create_nonce( 'conf_manager_public_csrf' ),
			'botGuard'  => $bot_challenge,
		] );

		wp_register_style(
			'conf-manager-public',
			CONF_MANAGER_PLUGIN_URL . 'public/css/public.css',
			[],
			CONF_MANAGER_VERSION
		);
	}
}
