<?php

namespace fluXtore\Core\Settings;

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

if ( ! class_exists( 'fluXtore_Google_Analytics_Settings' ) ) {
        class fluXtore_Google_Analytics_Settings {
                const OPTION_MEASUREMENT_ID   = 'google_analytics_measurement_id';
                const OPTION_PAGE_VIEW        = 'google_analytics_track_page_view';
                const OPTION_BEGIN_CHECKOUT   = 'google_analytics_track_begin_checkout';
                const OPTION_ADD_PAYMENT_INFO = 'google_analytics_track_add_payment_info';
                const OPTION_PURCHASE         = 'google_analytics_track_purchase';

                /**
                 * @var self|null
                 */
                private static $instance = null;

                private function __construct() {}

                public static function init() {
                        if ( null !== self::$instance ) {
                                return;
                        }

                        self::$instance = new self();
                        add_action( 'fluxtore_register_options', [ self::$instance, 'add_options' ] );
                        add_filter( 'fluxtore_settings_' . self::OPTION_MEASUREMENT_ID . '_save', [ self::$instance, 'sanitize_measurement_id' ] );
                }

                public function add_options() {
                        fluXtore_Settings::register_option( 'tracking', self::OPTION_MEASUREMENT_ID, [
                                'type'        => 'text',
                                'label'       => 'Google Analytics Measurement ID',
                                'section'     => 'google_analytics',
                                'placeholder' => 'G-XXXXXXXXXX',
                                'description' => 'Add your GA4 Measurement ID (e.g. G-XXXXXXXXXX) to enable tracking.',
                        ] );

                        fluXtore_Settings::register_option( 'tracking', self::OPTION_PAGE_VIEW, [
                                'type'    => 'bool',
                                'label'   => 'Track Page View event',
                                'section' => 'google_analytics',
                                'default' => $this->get_bool_default( self::OPTION_PAGE_VIEW, true ),
                        ] );

                        fluXtore_Settings::register_option( 'tracking', self::OPTION_BEGIN_CHECKOUT, [
                                'type'    => 'bool',
                                'label'   => 'Track Begin Checkout event',
                                'section' => 'google_analytics',
                                'default' => $this->get_bool_default( self::OPTION_BEGIN_CHECKOUT, true ),
                        ] );

                        fluXtore_Settings::register_option( 'tracking', self::OPTION_ADD_PAYMENT_INFO, [
                                'type'    => 'bool',
                                'label'   => 'Track Order Bump toggles (Add Payment Info event)',
                                'section' => 'google_analytics',
                                'default' => $this->get_bool_default( self::OPTION_ADD_PAYMENT_INFO, true ),
                        ] );

                        fluXtore_Settings::register_option( 'tracking', self::OPTION_PURCHASE, [
                                'type'    => 'bool',
                                'label'   => 'Track Purchase event',
                                'section' => 'google_analytics',
                                'default' => $this->get_bool_default( self::OPTION_PURCHASE, true ),
                        ] );
                }

                private function get_bool_default( $option, $default = false ) {
                        $raw = fluXtore_Settings::get( $option, $default ? '1' : '0' );

                        if ( is_bool( $raw ) ) {
                                return $raw;
                        }

                        if ( is_numeric( $raw ) ) {
                                return (bool) intval( $raw );
                        }

                        if ( is_string( $raw ) ) {
                                $raw = trim( $raw );

                                if ( '' === $raw ) {
                                        return (bool) $default;
                                }

                                $filtered = filter_var( $raw, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE );

                                if ( null !== $filtered ) {
                                        return $filtered;
                                }
                        }

                        return (bool) $default;
                }

                public function sanitize_measurement_id( $value ) {
                        if ( is_string( $value ) || is_numeric( $value ) ) {
                                $value = strtoupper( trim( (string) $value ) );
                                $value = preg_replace( '/\s+/', '', $value );

                                return $value;
                        }

                        return '';
                }
        }
}

fluXtore_Google_Analytics_Settings::init();
