<?php
/**
 * Configuration Manager Class
 *
 * @package ShopExplorer\Core
 * @since 1.0.0
 */

namespace SHOPEXPLORER\Core;

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

/**
 * Configuration Manager class
 *
 * Handles plugin configuration and mode settings
 *
 * @since 1.0.0
 */
class SHOPEX_ConfigurationManager {

	/**
	 * Option prefix
	 */
	const OPTION_PREFIX = 'shopex_';

	/**
	 * Default configuration
	 *
	 * @var array
	 */
	private $default_config = [
		'database_mode_enabled' => true,
		'multivendor_enabled' => false,
		'sync_interval' => 300, // 5 minutes
		'conflict_resolution_strategy' => 'timestamp',
		'custom_field_mappings' => [],
	];

	/**
	 * Constructor
	 */
	public function __construct() {
		$this->init_default_settings();
	}

	/**
	 * Initialize default settings if they don't exist
	 */
	private function init_default_settings() {
		foreach ( $this->default_config as $key => $value ) {
			$option_name = self::OPTION_PREFIX . $key;
			if ( false === get_option( $option_name ) ) {
				add_option( $option_name, $value );
			}
		}
	}

	/**
	 * Get configuration value
	 *
	 * @param string $key Configuration key
	 * @param mixed  $default Default value if not found
	 * @return mixed
	 */
	public function get( $key, $default = null ) {
		$option_name = self::OPTION_PREFIX . $key;
		$value = get_option( $option_name, $default );

		// Return default config value if option doesn't exist and no default provided
		if ( null === $default && isset( $this->default_config[ $key ] ) ) {
			return $this->default_config[ $key ];
		}

		return $value;
	}

	/**
	 * Set configuration value
	 *
	 * @param string $key Configuration key
	 * @param mixed  $value Configuration value
	 * @return bool
	 */
	public function set( $key, $value ) {
		$option_name = self::OPTION_PREFIX . $key;
		return update_option( $option_name, $value );
	}

	/**
	 * Get all mode settings
	 *
	 * @return array
	 */
	public function get_mode_settings() {
		return [
			'database_mode_enabled' => $this->is_database_mode_enabled(),
			'multivendor_enabled' => $this->is_multivendor_enabled(),
		];
	}

	/**
	 * Check if dual mode is enabled
	 *
	 * @return bool
	 */
	public function is_dual_mode_enabled() {
		return $this->is_database_mode_enabled();
	}

	/**
	 * Check if database mode is enabled
	 *
	 * @return bool
	 */
	public function is_database_mode_enabled() {
		return (bool) $this->get( 'database_mode_enabled', true );
	}

	/**
	 * Check if multivendor is enabled
	 *
	 * @return bool
	 */
	public function is_multivendor_enabled() {
		return (bool) $this->get( 'multivendor_enabled', false );
	}



	/**
	 * Get sync preferences
	 *
	 * @return array
	 */
	public function get_sync_preferences() {
		return [
			'sync_interval' => $this->get( 'sync_interval', 300 ),
			'conflict_resolution_strategy' => $this->get( 'conflict_resolution_strategy', 'timestamp' ),
		];
	}

	/**
	 * Validate configuration
	 *
	 * @param array $config Configuration to validate
	 * @return bool|array True if valid, array of errors if invalid
	 */
	public function validate_configuration( $config = null ) {
		if ( null === $config ) {
			$config = $this->get_all_settings();
		}

		$errors = [];

		// Validate that database mode is enabled
		if ( empty( $config['database_mode_enabled'] ) ) {
			$errors[] = __( 'Database mode must be enabled.', 'shop-explorer' );
		}

		// Validate sync interval
		$sync_interval = $config['sync_interval'] ?? 300;
		if ( ! is_numeric( $sync_interval ) || $sync_interval < 60 ) {
			$errors[] = __( 'Sync interval must be at least 60 seconds.', 'shop-explorer' );
		}

		return empty( $errors ) ? true : $errors;
	}

	/**
	 * Get all settings
	 *
	 * @return array
	 */
	public function get_all_settings() {
		$settings = [];
		foreach ( array_keys( $this->default_config ) as $key ) {
			$settings[ $key ] = $this->get( $key );
		}
		return $settings;
	}

	/**
	 * Reset to default configuration
	 *
	 * @return bool
	 */
	public function reset_to_defaults() {
		$success = true;
		foreach ( $this->default_config as $key => $value ) {
			if ( ! $this->set( $key, $value ) ) {
				$success = false;
			}
		}
		return $success;
	}

	/**
	 * Export configuration
	 *
	 * @return array
	 */
	public function export_configuration() {
		return $this->get_all_settings();
	}

	/**
	 * Import configuration
	 *
	 * @param array $config Configuration to import
	 * @return bool|array True if successful, array of errors if failed
	 */
	public function import_configuration( $config ) {
		// Validate configuration first
		$validation_result = $this->validate_configuration( $config );
		if ( true !== $validation_result ) {
			return $validation_result;
		}

		// Import valid configuration
		$success = true;
		foreach ( $config as $key => $value ) {
			if ( array_key_exists( $key, $this->default_config ) ) {
				if ( ! $this->set( $key, $value ) ) {
					$success = false;
				}
			}
		}

		return $success;
	}
}
