<?php
/**
 * Form Designer admin page.
 *
 * @package Donatico\Admin
 */

declare(strict_types=1);

namespace Donatico\Admin;

use Donatico\Models\Donation;

/**
 * Provides a live-preview interface for configuring the donation form's
 * visual appearance without touching the Settings page.
 *
 * Settings are shared with the Form tab of the Settings page — changes
 * here update the same wp_options values.
 */
class FormDesigner {

	// -------------------------------------------------------------------------
	// Save handler
	// -------------------------------------------------------------------------

	/**
	 * Handle the AJAX preview refresh request.
	 * Hooked to wp_ajax_donatico_designer_preview.
	 *
	 * @return void
	 */
	public function ajaxPreview(): void {
		check_ajax_referer( 'donatico_admin', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => __( 'Insufficient permissions.', 'donatico' ) ), 403 );
		}

		$layout  = isset( $_POST['layout'] ) ? sanitize_key( wp_unslash( $_POST['layout'] ) ) : 'stacked';
		$amounts = isset( $_POST['amounts'] ) ? sanitize_text_field( wp_unslash( $_POST['amounts'] ) ) : '5,10,25,50,100';
		$goal    = isset( $_POST['goal'] ) ? absint( $_POST['goal'] ) : 0;

		ob_start();
		$this->renderPreviewHtml( $layout, $amounts, $goal );
		$html = ob_get_clean();

		wp_send_json_success( array( 'html' => $html ) );
	}

	// -------------------------------------------------------------------------
	// Page render
	// -------------------------------------------------------------------------

	/**
	 * Render the Form Designer page.
	 * Callback registered via add_submenu_page().
	 *
	 * @return void
	 */
	public function render(): void {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'donatico' ) );
		}

		$current_layout  = get_option( 'donatico_form_layout', 'stacked' );
		$current_amounts = get_option( 'donatico_preset_amounts', array( 5, 10, 25, 50, 100 ) );
		$current_goal    = (int) get_option( 'donatico_goal_amount', 0 );
		$current_amounts = is_array( $current_amounts ) ? implode( ', ', $current_amounts ) : $current_amounts;
		$currency        = get_option( 'donatico_currency', 'USD' );

		include DONATICO_PLUGIN_DIR . 'templates/admin/form-designer.php';
	}

	// -------------------------------------------------------------------------
	// Preview renderer (shared by AJAX and initial render)
	// -------------------------------------------------------------------------

	/**
	 * Echo the preview HTML fragment for the given configuration.
	 *
	 * @param string $layout  One of inline|stacked|card.
	 * @param string $amounts Comma-separated dollar amounts.
	 * @param int    $goal    Goal in major currency unit.
	 * @return void
	 */
	public function renderPreviewHtml( string $layout, string $amounts, int $goal ): void {
		$layout = \Donatico\Frontend\FormRenderer::sanitizeLayout( $layout );

		$parsed_amounts = \Donatico\Frontend\FormRenderer::parseAmounts( $amounts );

		// Build every variable the form templates expect.
		$form_id         = 'donatico-preview-' . uniqid();
		$currency        = (string) get_option( 'donatico_currency', 'USD' );
		$currency_symbol = \Donatico\Helpers\Currency::getSymbol( $currency );
		$title           = '';
		$description     = '';
		$button_text     = __( 'Donate Now', 'donatico' );
		$show_message    = true;
		$allow_anonymous = true;
		$total_raised    = 0;
		$progress        = 0;
		$nonce           = ''; // preview — no real submission

		// Re-use $parsed_amounts as $amounts for template compatibility.
		$amounts = $parsed_amounts;

		$template = DONATICO_PLUGIN_DIR . 'templates/frontend/form-' . $layout . '.php';
		if ( file_exists( $template ) ) {
			include $template;
		} else {
			echo '<p class="description">' . esc_html__( 'Template not found.', 'donatico' ) . '</p>';
		}
	}
}
