<?php
/**
 * Form Editor Assets.
 *
 * Handles asset registration and enqueuing for the donation form editor.
 *
 * @package SureDonation
 * @since 0.0.1
 */

namespace SureDonation\Inc\FormEditor;

use SureDonation\Inc\Traits\Get_Instance;
use SureDonation\Inc\Post_Types\Donation_Form;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Assets Class.
 *
 * @since 0.0.1
 */
class Assets {
	use Get_Instance;

	/**
	 * Constructor.
	 *
	 * @since 0.0.1
	 */
	public function __construct() {
		add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_editor_assets' ] );
		add_action( 'init', [ $this, 'register_form_settings_meta' ] );
	}

	/**
	 * Enqueue editor assets for the donation form editor.
	 *
	 * @return void
	 * @since 0.0.1
	 */
	public function enqueue_editor_assets() {
		$screen = get_current_screen();

		// Only load on donation form editor.
		if ( ! $screen || Donation_Form::POST_TYPE !== $screen->post_type ) {
			return;
		}

		// Get the asset file for dependencies.
		$editor_asset_file = SUREDONATION_DIR . 'assets/build/editor/editor.asset.php';
		$editor_asset      = file_exists( $editor_asset_file )
			? include $editor_asset_file
			: [
				'dependencies' => [
					'wp-plugins',
					'wp-editor',
					'wp-components',
					'wp-data',
					'wp-element',
					'wp-i18n',
				],
				'version'      => SUREDONATION_VER,
			];

		// Editor plugin JS.
		wp_enqueue_script(
			'suredonation-form-editor',
			SUREDONATION_URL . 'assets/build/editor/editor.js',
			$editor_asset['dependencies'],
			$editor_asset['version'],
			true
		);

		// Editor styles.
		wp_enqueue_style(
			'suredonation-form-editor',
			SUREDONATION_URL . 'assets/build/editor/editor.css',
			[ 'wp-components' ],
			$editor_asset['version']
		);

		// Localized data.
		wp_localize_script(
			'suredonation-form-editor',
			'suredonationFormEditor',
			[
				'ajaxUrl'  => admin_url( 'admin-ajax.php' ),
				'nonce'    => wp_create_nonce( 'suredonation_editor_nonce' ),
				'postType' => Donation_Form::POST_TYPE,
			]
		);

		// Set script translations.
		wp_set_script_translations( 'suredonation-form-editor', 'suredonation' );
	}

	/**
	 * Register form settings meta fields.
	 *
	 * These meta fields store form-level settings like submit button text,
	 * success message, and confirmation type.
	 *
	 * @return void
	 * @since 0.0.1
	 */
	public function register_form_settings_meta() {
		$post_type = Donation_Form::POST_TYPE;

		// Submit button text.
		register_post_meta(
			$post_type,
			'_suredonation_form_submitButtonText',
			[
				'type'              => 'string',
				'description'       => __( 'Submit button text.', 'suredonation' ),
				'single'            => true,
				'default'           => 'Donate',
				'show_in_rest'      => true,
				'sanitize_callback' => 'sanitize_text_field',
				'auth_callback'     => function () {
					return current_user_can( 'edit_posts' );
				},
			]
		);

		// Success message.
		register_post_meta(
			$post_type,
			'_suredonation_form_successMessage',
			[
				'type'              => 'string',
				'description'       => __( 'Success message shown after donation.', 'suredonation' ),
				'single'            => true,
				'default'           => 'Thank you for your donation!',
				'show_in_rest'      => true,
				'sanitize_callback' => 'sanitize_textarea_field',
				'auth_callback'     => function () {
					return current_user_can( 'edit_posts' );
				},
			]
		);

		// Confirmation type.
		register_post_meta(
			$post_type,
			'_suredonation_form_confirmationType',
			[
				'type'              => 'string',
				'description'       => __( 'What happens after successful donation.', 'suredonation' ),
				'single'            => true,
				'default'           => 'message',
				'show_in_rest'      => true,
				'sanitize_callback' => function ( $value ) {
					$allowed = [ 'message', 'redirect' ];
					return in_array( $value, $allowed, true ) ? $value : 'message';
				},
				'auth_callback'     => function () {
					return current_user_can( 'edit_posts' );
				},
			]
		);

		// Redirect URL.
		register_post_meta(
			$post_type,
			'_suredonation_form_redirectUrl',
			[
				'type'              => 'string',
				'description'       => __( 'URL to redirect after donation.', 'suredonation' ),
				'single'            => true,
				'default'           => '',
				'show_in_rest'      => true,
				'sanitize_callback' => 'esc_url_raw',
				'auth_callback'     => function () {
					return current_user_can( 'edit_posts' );
				},
			]
		);
	}
}
