<?php
/**
 * Free plugin settings storage, sanitization, and admin UI.
 *
 * @package DreamFast_CF
 */

namespace DreamFast\CommerceFloat;

defined( 'ABSPATH' ) || exit;

/**
 * Class Settings
 */
class Settings {

	const OPTION_KEY = 'dreamfast_cf_settings';

	const MENU_SLUG = 'dreamfast-commerce-float';

	const CONTACT_MENU_SLUG = 'dreamfast-commerce-float-contact-widget';

	/**
	 * Singleton instance.
	 *
	 * @var Settings|null
	 */
	private static $instance = null;

	/**
	 * Cached settings for the current request.
	 *
	 * @var array<string, mixed>|null
	 */
	private static $settings_cache = null;

	/**
	 * Get singleton instance.
	 *
	 * @return Settings
	 */
	public static function instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Register hooks.
	 */
	public function init() {
		add_action( 'admin_menu', array( $this, 'register_admin_menu' ), 9 );
		add_action( 'admin_init', array( $this, 'register_settings' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_menu_icon_css' ) );
	}

	/**
	 * Default free settings.
	 *
	 * @return array<string, mixed>
	 */
	public static function get_defaults() {
		return array(
			'cart_enabled'            => true,
			'contact_enabled'         => true,
			'contact_tooltip_enabled' => true,
			'contact_tooltip_text'    => 'Need help?',
			'whatsapp_number'         => '',
			'whatsapp_message'        => '',
			'cart_bg_color'           => '#111111',
			'cart_text_color'         => '#ffffff',
			'cart_btn_color'          => '#ffffff',
			'cart_btn_text'           => '#111111',
			'cart_button_icon'        => 'cart',
			'cart_button_icon_pos'    => 'before',
			'cart_style'              => 'classic',
			'cart_preset'             => 'contrast',
			'contact_bg_color'        => '#25D366',
			'contact_icon_color'      => '#ffffff',
			'contact_style'           => 'circle',
			'contact_preset'          => 'whatsapp',
			'cart_animation'          => 'slide',
			'cart_position'           => 'bottom',
			'contact_animation'       => 'scale',
			'contact_position'        => 'bottom-right',
			'preview_direction'       => 'auto',
		);
	}

	/**
	 * Get merged settings.
	 *
	 * @return array<string, mixed>
	 */
	public static function get_all() {
		if ( null !== self::$settings_cache ) {
			return self::$settings_cache;
		}

		$stored = get_option( self::OPTION_KEY, array() );
		if ( ! is_array( $stored ) ) {
			$stored = array();
		}

		self::$settings_cache = array_merge( self::get_defaults(), $stored );

		return self::$settings_cache;
	}

	/**
	 * Clear the request cache.
	 */
	public static function flush_cache() {
		self::$settings_cache = null;
	}

	/**
	 * Get one setting.
	 *
	 * @param string $key      Setting key.
	 * @param mixed  $fallback Fallback value.
	 * @return mixed
	 */
	public static function get( $key, $fallback = null ) {
		$settings = self::get_all();

		return array_key_exists( $key, $settings ) ? $settings[ $key ] : $fallback;
	}

	/**
	 * Sticky cart button icon options.
	 *
	 * @return array<string, string>
	 */
	public static function get_cart_button_icon_options() {
		return array(
			'none'      => __( 'No icon', 'dreamfast-commerce-float' ),
			'cart'      => __( 'Cart', 'dreamfast-commerce-float' ),
			'bag'       => __( 'Shopping bag (outline)', 'dreamfast-commerce-float' ),
			'bag_solid' => __( 'Shopping bag (solid)', 'dreamfast-commerce-float' ),
			'basket'    => __( 'Shopping basket', 'dreamfast-commerce-float' ),
			'plus'      => __( 'Plus', 'dreamfast-commerce-float' ),
			'bolt'      => __( 'Bolt / quick purchase', 'dreamfast-commerce-float' ),
			'checkout'  => __( 'Checkout', 'dreamfast-commerce-float' ),
			'heart'     => __( 'Heart', 'dreamfast-commerce-float' ),
		);
	}

	/**
	 * Sticky cart visual styles.
	 *
	 * @return array<string, string>
	 */
	public static function get_cart_style_options() {
		return array(
			'classic' => __( 'Classic', 'dreamfast-commerce-float' ),
			'pill'    => __( 'Pill compact', 'dreamfast-commerce-float' ),
			'glass'   => __( 'Glass', 'dreamfast-commerce-float' ),
			'card'    => __( 'Card', 'dreamfast-commerce-float' ),
		);
	}

	/**
	 * Sticky cart color presets.
	 *
	 * @return array<string, array<string, string>>
	 */
	public static function get_cart_preset_options() {
		return array(
			'contrast' => array(
				'label'   => __( 'Contrast Black', 'dreamfast-commerce-float' ),
				'bg'      => '#111111',
				'text'    => '#ffffff',
				'btn'     => '#ffffff',
				'btnText' => '#111111',
			),
			'blue'     => array(
				'label'   => __( 'Conversion Blue', 'dreamfast-commerce-float' ),
				'bg'      => '#0f172a',
				'text'    => '#ffffff',
				'btn'     => '#2563eb',
				'btnText' => '#ffffff',
			),
			'green'    => array(
				'label'   => __( 'Fresh Green', 'dreamfast-commerce-float' ),
				'bg'      => '#052e1b',
				'text'    => '#ffffff',
				'btn'     => '#22c55e',
				'btnText' => '#052e1b',
			),
			'rose'     => array(
				'label'   => __( 'Luxury Rose', 'dreamfast-commerce-float' ),
				'bg'      => '#4a102a',
				'text'    => '#ffffff',
				'btn'     => '#f9a8d4',
				'btnText' => '#4a102a',
			),
			'gold'     => array(
				'label'   => __( 'Premium Gold', 'dreamfast-commerce-float' ),
				'bg'      => '#292524',
				'text'    => '#fff7ed',
				'btn'     => '#fbbf24',
				'btnText' => '#292524',
			),
			'light'    => array(
				'label'   => __( 'Clean Light', 'dreamfast-commerce-float' ),
				'bg'      => '#ffffff',
				'text'    => '#111827',
				'btn'     => '#111827',
				'btnText' => '#ffffff',
			),
		);
	}

	/**
	 * Contact widget styles.
	 *
	 * @return array<string, string>
	 */
	public static function get_contact_style_options() {
		return array(
			'circle' => __( 'Circle', 'dreamfast-commerce-float' ),
			'pill'   => __( 'Pill', 'dreamfast-commerce-float' ),
			'square' => __( 'Rounded square', 'dreamfast-commerce-float' ),
			'soft'   => __( 'Soft glow', 'dreamfast-commerce-float' ),
		);
	}

	/**
	 * Contact widget color presets.
	 *
	 * @return array<string, array<string, string>>
	 */
	public static function get_contact_preset_options() {
		return array(
			'whatsapp' => array(
				'label' => 'Green',
				'bg'    => '#25D366',
				'icon'  => '#ffffff',
			),
			'dark'     => array(
				'label' => __( 'Dark', 'dreamfast-commerce-float' ),
				'bg'    => '#111827',
				'icon'  => '#ffffff',
			),
			'blue'     => array(
				'label' => __( 'Blue', 'dreamfast-commerce-float' ),
				'bg'    => '#2563eb',
				'icon'  => '#ffffff',
			),
			'rose'     => array(
				'label' => __( 'Rose', 'dreamfast-commerce-float' ),
				'bg'    => '#e11d48',
				'icon'  => '#ffffff',
			),
			'gold'     => array(
				'label' => __( 'Gold', 'dreamfast-commerce-float' ),
				'bg'    => '#f59e0b',
				'icon'  => '#111827',
			),
		);
	}

	/**
	 * Whether WooCommerce Blocks classes are present.
	 *
	 * @return bool
	 */
	public static function has_woocommerce_blocks_support() {
		return class_exists( 'Automattic\WooCommerce\Blocks\Package' )
			|| class_exists( 'Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFields' )
			|| function_exists( 'woocommerce_blocks_loaded' );
	}

	/**
	 * Sanitize an option key against a known list.
	 *
	 * @param string               $value    Raw value.
	 * @param array<string, mixed> $options  Allowed options.
	 * @param string               $fallback Fallback.
	 * @return string
	 */
	private static function sanitize_option_key( $value, $options, $fallback ) {
		$value = sanitize_key( (string) $value );

		return array_key_exists( $value, $options ) ? $value : $fallback;
	}

	/**
	 * Sanitize selected sticky cart icon.
	 *
	 * @param string $icon Icon slug.
	 * @return string
	 */
	public static function sanitize_cart_button_icon( $icon ) {
		return self::sanitize_option_key( $icon, self::get_cart_button_icon_options(), 'cart' );
	}

	/**
	 * Get the selected button icon HTML.
	 *
	 * @return string
	 */
	public static function get_cart_button_icon_html() {
		return self::get_cart_button_icon_svg( (string) self::get( 'cart_button_icon', 'cart' ) );
	}

	/**
	 * Get a trusted inline SVG for the selected button icon.
	 *
	 * @param string $icon Icon slug.
	 * @return string
	 */
	public static function get_cart_button_icon_svg( $icon ) {
		$icon = self::sanitize_cart_button_icon( $icon );
		if ( 'none' === $icon ) {
			return '';
		}

		$paths = array(
			'cart'      => '<path d="M7 18c-1.1 0-1.99.9-1.99 2S5.9 22 7 22s2-.9 2-2-.9-2-2-2ZM1 2v2h2l3.6 7.59-1.35 2.44C4.52 15.37 5.48 17 7 17h12v-2H7l1.1-2h7.45c.75 0 1.41-.41 1.75-1.03L21 5H5.21l-.94-2H1Zm16 16c-1.1 0-1.99.9-1.99 2S15.9 22 17 22s2-.9 2-2-.9-2-2-2Z"/>',
			'bag'       => '<path d="M6 7V6a6 6 0 0 1 12 0v1h2v15H4V7h2Zm2 0h8V6a4 4 0 0 0-8 0v1Zm-2 2v11h12V9h-2v2h-2V9H8v2H6V9Z"/>',
			'bag_solid' => '<path d="M18 6h-2A4 4 0 0 0 8 6H6a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2zm-6-2a2 2 0 0 1 2 2h-4a2 2 0 0 1 2-2zm6 16H6V8h2v2a1 1 0 0 0 2 0V8h4v2a1 1 0 0 0 2 0V8h2v12z"/>',
			'basket'    => '<path d="M22 9h-4.79l-4.38-6.56c-.19-.28-.51-.42-.83-.42s-.64.14-.83.43L6.79 9H2c-.55 0-1 .45-1 1 0 .09.01.18.04.27l2.54 9.27c.23.84 1 1.46 1.92 1.46h13c.92 0 1.69-.62 1.93-1.46l2.54-9.27.03-.27c0-.55-.45-1-1-1zm-10-4.79L15.19 9H8.81L12 4.21zM12 18c-1.1 0-2-.9-2-2s.9-2 2-2 2 .9 2 2-.9 2-2 2z"/>',
			'plus'      => '<path d="M19 11h-6V5h-2v6H5v2h6v6h2v-6h6v-2Z"/>',
			'bolt'      => '<path d="M13 2 4 14h7l-1 8 10-13h-7l1-7Z"/>',
			'checkout'  => '<path d="M9 16.2 4.8 12l-1.4 1.4L9 19 21 7l-1.4-1.4L9 16.2Z"/>',
			'heart'     => '<path d="M12 21.35 10.55 20.03C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09A6.04 6.04 0 0 1 16.5 3C19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35Z"/>',
		);

		return '<svg viewBox="0 0 24 24" width="18" height="18" aria-hidden="true" focusable="false">' . $paths[ $icon ] . '</svg>';
	}

	/**
	 * SVG map for the admin preview.
	 *
	 * @return array<string, string>
	 */
	public static function get_cart_button_icon_svg_map() {
		$icons = array();
		foreach ( array_keys( self::get_cart_button_icon_options() ) as $icon ) {
			$icons[ $icon ] = self::get_cart_button_icon_svg( $icon );
		}

		return $icons;
	}

	/**
	 * Whether the sticky cart should render.
	 *
	 * @return bool
	 */
	public static function should_show_sticky_cart() {
		$show = (bool) self::get( 'cart_enabled' )
			&& class_exists( 'WooCommerce' )
			&& function_exists( 'is_product' )
			&& is_product();

		/**
		 * Filter whether the sticky add-to-cart bar renders on the current request.
		 *
		 * Use this to add custom display rules, for example hiding the bar on
		 * specific products, categories, or templates.
		 *
		 * @since 1.1.0
		 *
		 * @param bool $show Whether the sticky cart bar should render.
		 */
		return (bool) apply_filters( 'dreamfast_cf_show_sticky_cart', $show );
	}

	/**
	 * Whether the WhatsApp contact widget should render.
	 *
	 * @return bool
	 */
	public static function should_show_contact() {
		$show = (bool) self::get( 'contact_enabled' )
			&& '' !== self::normalize_phone( (string) self::get( 'whatsapp_number' ) );

		/**
		 * Filter whether the floating contact button renders on the current request.
		 *
		 * @since 1.1.0
		 *
		 * @param bool $show Whether the contact widget should render.
		 */
		return (bool) apply_filters( 'dreamfast_cf_show_contact', $show );
	}

	/**
	 * Normalize a phone number for links.
	 *
	 * @param string $phone Raw phone.
	 * @return string
	 */
	public static function normalize_phone( $phone ) {
		$phone = trim( (string) $phone );
		if ( '' === $phone ) {
			return '';
		}

		$has_plus = 0 === strpos( $phone, '+' );
		$digits   = preg_replace( '/\D+/', '', $phone );
		if ( ! is_string( $digits ) || '' === $digits ) {
			return '';
		}

		$phone = $has_plus ? '+' . $digits : $digits;

		return strlen( $phone ) <= 32 ? $phone : '';
	}

	/**
	 * Build the configured WhatsApp URL.
	 *
	 * @return string
	 */
	public static function get_whatsapp_url() {
		$phone = self::normalize_phone( (string) self::get( 'whatsapp_number' ) );
		if ( '' === $phone ) {
			return '';
		}

		$url     = 'https://wa.me/' . rawurlencode( ltrim( $phone, '+' ) );
		$message = trim( (string) self::get( 'whatsapp_message' ) );
		if ( '' !== $message ) {
			$url .= '?text=' . rawurlencode( $message );
		}

		return $url;
	}

	/**
	 * Frontend CSS variables.
	 *
	 * @return string
	 */
	public static function get_css_vars() {
		$settings       = self::get_all();
		$cart_presets   = self::get_cart_preset_options();
		$contact_presets = self::get_contact_preset_options();
		$cart_preset    = self::sanitize_option_key( (string) $settings['cart_preset'], $cart_presets, 'contrast' );
		$contact_preset = self::sanitize_option_key( (string) $settings['contact_preset'], $contact_presets, 'whatsapp' );
		$cart            = $cart_presets[ $cart_preset ];
		$contact         = $contact_presets[ $contact_preset ];

		return '--df-sf-cart-bg:' . $cart['bg'] . ';'
			. '--df-sf-cart-text:' . $cart['text'] . ';'
			. '--df-sf-cart-btn-bg:' . $cart['btn'] . ';'
			. '--df-sf-cart-btn-text:' . $cart['btnText'] . ';'
			. '--df-sf-contact-bg:' . $contact['bg'] . ';'
			. '--df-sf-contact-icon:' . $contact['icon'] . ';';
	}

	/**
	 * Sticky cart wrapper classes.
	 *
	 * @return string
	 */
	public static function get_cart_theme_classes() {
		$style  = self::sanitize_option_key( (string) self::get( 'cart_style', 'classic' ), self::get_cart_style_options(), 'classic' );
		$preset = self::sanitize_option_key( (string) self::get( 'cart_preset', 'contrast' ), self::get_cart_preset_options(), 'contrast' );

		return 'df-sf-sticky-cart--style-' . $style . ' df-sf-sticky-cart--preset-' . $preset;
	}

	/**
	 * Contact widget wrapper classes.
	 *
	 * @return string
	 */
	public static function get_contact_theme_classes() {
		$style  = self::sanitize_option_key( (string) self::get( 'contact_style', 'circle' ), self::get_contact_style_options(), 'circle' );
		$preset = self::sanitize_option_key( (string) self::get( 'contact_preset', 'whatsapp' ), self::get_contact_preset_options(), 'whatsapp' );

		return 'df-sf-contact--style-' . $style . ' df-sf-contact--preset-' . $preset;
	}

	/**
	 * Register plugin admin pages.
	 */
	public function register_admin_menu() {
		add_menu_page(
			__( 'DreamFast Commerce Float', 'dreamfast-commerce-float' ),
			__( 'DreamFast Commerce Float', 'dreamfast-commerce-float' ),
			'manage_options',
			self::MENU_SLUG,
			array( $this, 'render_dashboard_page' ),
			'dashicons-cart',
			56
		);

		add_submenu_page(
			self::MENU_SLUG,
			__( 'Dashboard', 'dreamfast-commerce-float' ),
			__( 'Dashboard', 'dreamfast-commerce-float' ),
			'manage_options',
			self::MENU_SLUG,
			array( $this, 'render_dashboard_page' )
		);

		add_submenu_page(
			self::MENU_SLUG,
			__( 'Sticky Cart Bar', 'dreamfast-commerce-float' ),
			__( 'Sticky Cart Bar', 'dreamfast-commerce-float' ),
			'manage_options',
			self::MENU_SLUG . '-cart',
			array( $this, 'render_cart_page' )
		);

		add_submenu_page(
			self::MENU_SLUG,
			__( 'Contact Widget', 'dreamfast-commerce-float' ),
			__( 'Contact Widget', 'dreamfast-commerce-float' ),
			'manage_options',
			self::CONTACT_MENU_SLUG,
			array( $this, 'render_contact_page' )
		);

		add_submenu_page(
			self::MENU_SLUG,
			__( 'Changelog', 'dreamfast-commerce-float' ),
			__( 'Changelog', 'dreamfast-commerce-float' ),
			'manage_options',
			self::MENU_SLUG . '-changelog',
			array( $this, 'render_changelog_page' )
		);
	}

	/**
	 * Apply the plugin menu icon and separators via an enqueued inline style.
	 */
	public function enqueue_menu_icon_css() {
		$slug = esc_attr( self::MENU_SLUG );
		$css  = sprintf(
			'#toplevel_page_%1$s .wp-menu-image::before{content:"\f174";}' .
			'#toplevel_page_%1$s .wp-submenu li:nth-child(2),' .
			'#toplevel_page_%1$s .wp-submenu li:nth-child(4)' .
			'{margin-bottom:6px;padding-bottom:6px;border-bottom:1px solid rgba(240,246,252,0.18);}',
			$slug
		);

		wp_add_inline_style( 'admin-menu', $css );
	}

	/**
	 * Register the Settings API option.
	 */
	public function register_settings() {
		register_setting(
			'dreamfast_cf_settings_group',
			self::OPTION_KEY,
			array(
				'type'              => 'array',
				'sanitize_callback' => array( $this, 'sanitize_settings' ),
				'default'           => self::get_defaults(),
			)
		);
	}

	/**
	 * Sanitize free settings.
	 *
	 * @param mixed $input Raw submitted settings.
	 * @return array<string, mixed>
	 */
	public function sanitize_settings( $input ) {
		if ( ! current_user_can( 'manage_options' ) ) {
			return self::get_all();
		}

		if ( ! is_array( $input ) ) {
			return self::get_defaults();
		}

		$stored = get_option( self::OPTION_KEY, array() );
		if ( ! is_array( $stored ) ) {
			$stored = array();
		}

		$output = array_merge( self::get_defaults(), $stored );
		$text   = static function ( $key, $max_length, $multiline = false ) use ( $input ) {
			if ( ! isset( $input[ $key ] ) || ! is_scalar( $input[ $key ] ) ) {
				return '';
			}

			$value = $multiline
				? sanitize_textarea_field( (string) $input[ $key ] )
				: sanitize_text_field( (string) $input[ $key ] );

			return substr( $value, 0, $max_length );
		};

		foreach ( array( 'cart_enabled', 'contact_enabled', 'contact_tooltip_enabled' ) as $key ) {
			if ( array_key_exists( $key, $input ) ) {
				$output[ $key ] = ! empty( $input[ $key ] );
			}
		}

		if ( array_key_exists( 'contact_tooltip_text', $input ) ) {
			$output['contact_tooltip_text'] = $text( 'contact_tooltip_text', 60 );
		}
		if ( array_key_exists( 'whatsapp_number', $input ) ) {
			$output['whatsapp_number'] = $text( 'whatsapp_number', 32 );
		}
		if ( array_key_exists( 'whatsapp_message', $input ) ) {
			$output['whatsapp_message'] = $text( 'whatsapp_message', 500, true );
		}
		if ( array_key_exists( 'cart_button_icon', $input ) ) {
			$output['cart_button_icon'] = self::sanitize_cart_button_icon( $input['cart_button_icon'] );
		}
		if ( array_key_exists( 'cart_button_icon_pos', $input ) ) {
			$icon_position                  = sanitize_key( (string) $input['cart_button_icon_pos'] );
			$output['cart_button_icon_pos'] = in_array( $icon_position, array( 'before', 'after' ), true ) ? $icon_position : 'before';
		}
		if ( array_key_exists( 'cart_style', $input ) ) {
			$output['cart_style'] = self::sanitize_option_key( $input['cart_style'], self::get_cart_style_options(), 'classic' );
		}
		if ( array_key_exists( 'cart_preset', $input ) ) {
			$output['cart_preset'] = self::sanitize_option_key( $input['cart_preset'], self::get_cart_preset_options(), 'contrast' );
			$preset                = self::get_cart_preset_options()[ $output['cart_preset'] ];
			$output['cart_bg_color']   = $preset['bg'];
			$output['cart_text_color'] = $preset['text'];
			$output['cart_btn_color']  = $preset['btn'];
			$output['cart_btn_text']   = $preset['btnText'];
		}
		if ( array_key_exists( 'contact_style', $input ) ) {
			$output['contact_style'] = self::sanitize_option_key( $input['contact_style'], self::get_contact_style_options(), 'circle' );
		}
		if ( array_key_exists( 'contact_preset', $input ) ) {
			$output['contact_preset'] = self::sanitize_option_key( $input['contact_preset'], self::get_contact_preset_options(), 'whatsapp' );
			$preset                   = self::get_contact_preset_options()[ $output['contact_preset'] ];
			$output['contact_bg_color']   = $preset['bg'];
			$output['contact_icon_color'] = $preset['icon'];
		}
		if ( array_key_exists( 'cart_position', $input ) ) {
			$position               = sanitize_key( (string) $input['cart_position'] );
			$output['cart_position'] = in_array( $position, array( 'auto', 'top', 'bottom' ), true ) ? $position : 'auto';
		}
		if ( array_key_exists( 'contact_position', $input ) ) {
			$position                  = sanitize_key( (string) $input['contact_position'] );
			$output['contact_position'] = in_array( $position, array( 'bottom-right', 'bottom-left' ), true ) ? $position : 'bottom-right';
		}
		if ( array_key_exists( 'cart_animation', $input ) ) {
			$animation                = sanitize_key( (string) $input['cart_animation'] );
			$output['cart_animation'] = in_array( $animation, array( 'slide', 'fade' ), true ) ? $animation : 'slide';
		}
		if ( array_key_exists( 'contact_animation', $input ) ) {
			$animation                   = sanitize_key( (string) $input['contact_animation'] );
			$output['contact_animation'] = in_array( $animation, array( 'scale', 'fade' ), true ) ? $animation : 'scale';
		}
		if ( array_key_exists( 'preview_direction', $input ) ) {
			$output['preview_direction'] = self::sanitize_option_key(
				$input['preview_direction'],
				array(
					'auto' => true,
					'rtl'  => true,
					'ltr'  => true,
				),
				'auto'
			);
		}

		self::flush_cache();

		return $output;
	}

	/**
	 * Enqueue plugin admin assets only on plugin pages.
	 *
	 * @param string $hook Current admin hook.
	 */
	public function enqueue_admin_assets( $hook ) {
		$allowed_hooks = array(
			'toplevel_page_' . self::MENU_SLUG,
			self::MENU_SLUG . '_page_' . self::MENU_SLUG . '-cart',
			self::MENU_SLUG . '_page_' . self::CONTACT_MENU_SLUG,
			self::MENU_SLUG . '_page_' . self::MENU_SLUG . '-changelog',
		);

		if ( ! in_array( $hook, $allowed_hooks, true ) ) {
			return;
		}

		wp_enqueue_style( 'dreamfast-cf-admin', DREAMFAST_CF_URL . 'assets/css/admin.css', array(), DREAMFAST_CF_VERSION );

		$settings_hooks = array(
			self::MENU_SLUG . '_page_' . self::MENU_SLUG . '-cart',
			self::MENU_SLUG . '_page_' . self::CONTACT_MENU_SLUG,
		);
		if ( ! in_array( $hook, $settings_hooks, true ) ) {
			return;
		}

		wp_enqueue_script( 'dreamfast-cf-admin', DREAMFAST_CF_URL . 'assets/js/admin.js', array( 'jquery' ), DREAMFAST_CF_VERSION, true );
		wp_localize_script(
			'dreamfast-cf-admin',
			'dreamfastCfAdmin',
			array(
				'cartPresets'    => self::get_cart_preset_options(),
				'contactPresets' => self::get_contact_preset_options(),
				'icons'          => self::get_cart_button_icon_svg_map(),
			)
		);
	}

	/**
	 * Render the dashboard.
	 */
	public function render_dashboard_page() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$settings = self::get_all();

		include DREAMFAST_CF_PATH . 'templates/admin-dashboard.php';
	}

	/**
	 * Render sticky cart settings.
	 */
	public function render_cart_page() {
		$this->render_settings_page( 'cart' );
	}

	/**
	 * Render contact widget settings.
	 */
	public function render_contact_page() {
		$this->render_settings_page( 'contact' );
	}

	/**
	 * Render a free settings page.
	 *
	 * @param string $active_tab Settings section.
	 */
	private function render_settings_page( $active_tab ) {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$settings   = self::get_all();
		$active_tab = 'contact' === $active_tab ? 'contact' : 'cart';
		$page_title = 'contact' === $active_tab
			? __( 'Contact Widget', 'dreamfast-commerce-float' )
			: __( 'Sticky Cart Bar', 'dreamfast-commerce-float' );
		$page_intro = 'contact' === $active_tab
			? __( 'Configure the floating contact button.', 'dreamfast-commerce-float' )
			: __( 'Configure the sticky add-to-cart bar for WooCommerce product pages.', 'dreamfast-commerce-float' );

		include DREAMFAST_CF_PATH . 'templates/admin-settings.php';
	}

	/**
	 * Render the changelog.
	 */
	public function render_changelog_page() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$changelog_path = DREAMFAST_CF_PATH . 'CHANGELOG.md';
		// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Reading a bundled local file.
		$changelog = is_readable( $changelog_path ) ? (string) file_get_contents( $changelog_path ) : '';

		include DREAMFAST_CF_PATH . 'templates/admin-changelog.php';
	}
}
