<?php
/**
 * Frontend asset and REST bootstrap.
 *
 * @package VoidPulse
 */

namespace VoidPulse\Frontend;

use VoidPulse\Shared\Asset;
use VoidPulse\Shared\GoogleFonts;

use VoidPulse\Infrastructure\NotificationSettings;

defined( 'ABSPATH' ) || exit;

final class Frontend {
	/**
	 * Register frontend hooks.
	 *
	 * @return void
	 */
	public function register() {
		add_action( 'rest_api_init', array( new RestController(), 'register' ) );
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ) );
	}

	/**
	 * Load only for an eligible active campaign.
	 *
	 * @return void
	 */
	public function enqueue() {
		$campaign = ( new NotificationSettings() )->get();
		if ( ! $campaign || 'active' !== $campaign['status'] || ! $this->page_is_eligible( $campaign ) ) {
			return;
		}
		$show_to_admins = ! isset( $campaign['performance']['show_to_admins'] ) || rest_sanitize_boolean( $campaign['performance']['show_to_admins'] );
		if ( current_user_can( 'manage_options' ) && ! $show_to_admins ) {
			return;
		}
		$asset = Asset::metadata( 'frontend' );
		wp_enqueue_style( 'voidpulse-frontend', VOIDPULSE_URL . 'public/frontend.css', array(), $asset['version'] );
		wp_style_add_data( 'voidpulse-frontend', 'rtl', 'replace' );
		$fonts_url = GoogleFonts::css_url_from_style( is_array( $campaign['style'] ) ? $campaign['style'] : array() );
		if ( $fonts_url ) {
			wp_enqueue_style( 'voidpulse-google-fonts', $fonts_url, array(), substr( md5( $fonts_url ), 0, 12 ) );
		}
		wp_enqueue_script( 'voidpulse-frontend', VOIDPULSE_URL . 'public/frontend.js', $asset['dependencies'], $asset['version'], true );
		$display       = isset( $campaign['display'] ) && is_array( $campaign['display'] ) ? $campaign['display'] : array();
		$public_config = apply_filters(
			'voidpulse_frontend_campaigns',
			array(
				'rest'     => esc_url_raw( rest_url( 'voidpulse/v1/frontend/' ) ),
				'template' => $campaign['template'],
				'delivery' => array(
					'frequency_cap'     => $campaign['performance']['frequency_cap'],
					'dismissal_snooze'  => $campaign['performance']['dismissal_snooze'],
					'repeat_protection' => $campaign['performance']['repeat_protection'],
					'cross_page_pacing' => $campaign['performance']['cross_page_pacing'],
					'optimized_loading' => $campaign['performance']['optimized_loading'],
					'data_saver'        => $campaign['performance']['data_saver'],
				),
				'timing'   => $campaign['timing'],
				'style'    => $campaign['style'],
				'display'  => array(
					'hide_mobile'  => ! empty( $display['hide_mobile'] ),
					'hide_desktop' => ! empty( $display['hide_desktop'] ),
				),
				'labels'   => array(
					'close'    => __( 'Close notification', 'voidpulse-social-proof-for-woocommerce' ),
					'activity' => __( 'Recent store activity', 'voidpulse-social-proof-for-woocommerce' ),
					'headline' => sanitize_text_field( isset( $campaign['name'] ) ? $campaign['name'] : __( 'Recent sales', 'voidpulse-social-proof-for-woocommerce' ) ),
				),
			),
			$campaign
		);
		wp_add_inline_script(
			'voidpulse-frontend',
			'window.voidPulse=' . wp_json_encode( $public_config, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT ) . ';',
			'before'
		);
	}

	/**
	 * Apply basic display rules.
	 *
	 * @param array<string,mixed> $campaign Campaign.
	 * @return bool
	 */
	private function page_is_eligible( $campaign ) {
		$display = $campaign['display'];
		if ( ( $display['hide_cart'] && is_cart() ) || ( $display['hide_checkout'] && is_checkout() ) || ( $display['hide_account'] && is_account_page() ) || ( $display['hide_received'] && function_exists( 'is_order_received_page' ) && is_order_received_page() ) ) {
			return false;
		}
		if ( ( $display['hide_mobile'] && wp_is_mobile() ) || ( $display['hide_desktop'] && ! wp_is_mobile() ) ) {
			return false;
		}
		if ( 'products' === $display['scope'] && ! is_product() ) {
			return false;
		}
		if ( 'archives' === $display['scope'] && ! ( is_shop() || is_product_taxonomy() ) ) {
			return false;
		}
		return (bool) apply_filters( 'voidpulse_campaign_is_eligible', true, $campaign );
	}
}
