<?php

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
/**
 * The admin-specific functionality of the plugin.
 *
 * @link       https://webequipe.com
 * @since      1.0.0
 *
 * @package    Webequipe_Spin_Win_Wheel
 * @subpackage Webequipe_Spin_Win_Wheel/admin
 */

include_once plugin_dir_path( __FILE__ ) . '/../includes/Form_Error.php';

use Webequipe\SpinWinWheel\Traits\Form_Error;

/**
 * Settings Handler class.
 */
class Webequipe_Spin_Win_Wheel_Settings {

	use Form_Error;

	/**
	 * Holds the values to be used in the fields callbacks.
	 *
	 * @var array
	 */
	private $options;

	/**
	 * Start up.
	 */
	public function __construct() {
		add_action( 'admin_init', [ $this, 'form_handler' ] );
		add_action( 'admin_init', [ $this, 'register_settings' ] );
	}

	/**
	 * Plugin page handler.
	 *
	 * @return void
	 */
	public function settings_page() {
		$template = __DIR__ . '/partials/webequipe-spin-win-wheel-settings.php';

		if ( file_exists( $template ) ) {
			include $template;
		}
	}

	/**
	 * Handle the settings form submission securely.
	 *
	 * @return void
	 */
	public function form_handler() {
		// Left intentionally empty for now – legacy settings form is disabled.
	}

	/**
	 * Register Mailchimp settings using the Settings API.
	 *
	 * @return void
	 */
	public function register_settings() {
		register_setting(
			'webequipe_sww_mailchimp',
			'wsww_mailchimp_settings',
			[ $this, 'sanitize_mailchimp_settings' ]
		);
	}

	/**
	 * Sanitize Mailchimp settings payload.
	 *
	 * @param array $input Raw submitted settings.
	 * @return array
	 */
	public function sanitize_mailchimp_settings( $input ) {
		$output = [];

		if ( ! is_array( $input ) ) {
			$input = [];
		}

		$existing_settings = get_option( 'wsww_mailchimp_settings', [] );
		if ( ! is_array( $existing_settings ) ) {
			$existing_settings = [];
		}

		$output['enabled'] = ! empty( $input['enabled'] ) ? 1 : 0;

		$submitted_api_key = isset( $input['api_key'] )
			? sanitize_text_field( wp_unslash( $input['api_key'] ) )
			: '';

		$existing_api_key = isset( $existing_settings['api_key'] )
			? sanitize_text_field( (string) $existing_settings['api_key'] )
			: '';

		if ( '' !== $submitted_api_key ) {
			if ( class_exists( '\Webequipe\SpinWinWheel\Mailchimp' ) ) {
				$output['api_key'] = \Webequipe\SpinWinWheel\Mailchimp::encrypt_api_key_for_storage( $submitted_api_key );
			} else {
				$output['api_key'] = $submitted_api_key;
			}
		} else {
			$retained_api_key = $existing_api_key;
			if (
				'' !== $retained_api_key
				&& class_exists( '\Webequipe\SpinWinWheel\Mailchimp' )
				&& ! \Webequipe\SpinWinWheel\Mailchimp::is_api_key_encrypted( $retained_api_key )
			) {
				$retained_api_key = \Webequipe\SpinWinWheel\Mailchimp::encrypt_api_key_for_storage( $retained_api_key );
			}
			$output['api_key'] = $retained_api_key;
		}

		$output['list_id'] = isset( $input['list_id'] )
			? sanitize_text_field( wp_unslash( $input['list_id'] ) )
			: '';

		$output['tag'] = isset( $input['tag'] )
			? sanitize_text_field( wp_unslash( $input['tag'] ) )
			: 'SpinWheel';

		$output['consent_enabled'] = ! empty( $input['consent_enabled'] ) ? 1 : 0;

		$default_consent_text = __( 'I agree to receive promotional emails.', 'webequipe-spin-win-wheel' );
		$output['consent_text']   = isset( $input['consent_text'] )
			? sanitize_text_field( wp_unslash( $input['consent_text'] ) )
			: $default_consent_text;

		// Preserve last_status if present.
		if ( isset( $input['last_status'] ) ) {
			$output['last_status'] = sanitize_text_field( wp_unslash( $input['last_status'] ) );
		}

		return $output;
	}

}
