<?php
namespace PageVICO\Settings;

defined( 'ABSPATH' ) || exit;

class SettingsManager {
	// Define all settings with their default values
	private static $default_settings = array(
		'PageVICO_enable_tracking'     => 1,
		'PageVICO_short_number'        => 0,
		'PageVICO_post_types'          => array( 'post', 'page' ),
		'PageVICO_block_posts'         => array(),
		'PageVICO_block_ips'           => '',
		'PageVICO_geo_db_last_updated' => 0,
		'PageVICO_enable_heatmap'      => 0,
		'PageVICO_analytics_retention' => 90,
		'PageVICO_heatmap_retention'   => 30,
		'ignore_roles'                 => array('administrator'),
		'ignore_ips'                   => array(),
		'is_tracking_logged_in'        => true,
		'enable_public_counter'        => false,
		'counter_position'             => 'after',
		'counter_label'                => 'Views:',
		'excluded_post_types'          => array(),
		'data_retention_months'        => 12,
		'session_timeout_minutes'      => 30,
	);

	/**
	 * Initialize default settings on plugin activation
	 */
	public static function initializeSettings() {
		foreach ( self::$default_settings as $option_name => $default_value ) {
			if ( get_option( $option_name ) === false ) {
				// Special case for translated strings
				if ($option_name === 'counter_label') {
					$default_value = __('Views:', 'page-visit-counter-analytics');
				}
				add_option( $option_name, $default_value );
			}
		}
	}

	/**
	 * Reset all settings to default values
	 */
	public static function resetSettings() {
		foreach ( self::$default_settings as $option_name => $default_value ) {
			update_option( $option_name, $default_value );
		}
	}

	/**
	 * Get default value for a setting
	 */
	public static function getDefaultValue( $setting_name ) {
		return self::$default_settings[ $setting_name ] ?? null;
	}

	/**
	 * Register all settings
	 */
	public static function registerSettings() {
		foreach ( self::$default_settings as $option_name => $default_value ) {
			$args = array(
				'type'              => 'string',
				'sanitize_callback' => function ( $input ) use ( $option_name ) {
					switch ( $option_name ) {
						case 'PageVICO_enable_tracking':
						case 'PageVICO_short_number':
							return absint( $input );

						case 'PageVICO_post_types':
							return is_array( $input ) ? array_map( 'sanitize_text_field', $input ) : array( 'post' );

						case 'PageVICO_block_posts':
							return is_array( $input ) ? array_map( 'absint', $input ) : array();

						case 'PageVICO_block_ips':
							return sanitize_textarea_field( $input );

						case 'PageVICO_geo_db_last_updated':
							return absint( $input );

						case 'PageVICO_enable_heatmap':
							return absint( $input );

						case 'PageVICO_analytics_retention':
							return absint( $input );

						case 'PageVICO_heatmap_retention':
							return absint( $input );

						case 'ignore_roles':
						case 'ignore_ips':
						case 'is_tracking_logged_in':
						case 'enable_public_counter':
						case 'counter_position':
						case 'counter_label':
						case 'excluded_post_types':
						case 'data_retention_months':
							return is_array( $input ) ? array_map( 'sanitize_text_field', $input ) : sanitize_text_field( $input );

						default:
							return sanitize_text_field( $input );
					}
				},
			);
			register_setting( 'PageVICO_settings', $option_name, $args );
		}
	}
}
