<?php

declare( strict_types=1 );

namespace StrykeWP\Core;

/**
 * Centralised options service for STRYKE WP.
 *
 * All options are prefixed with `stryke_wp_` to avoid collisions.
 * Provides module-scoped helpers so each module keeps its own keyspace.
 *
 * @package StrykeWP\Core
 */
class OptionManager {

	public const PREFIX = 'stryke_wp_';

	// ---------------------------------------------------------------------------
	// Generic option API
	// ---------------------------------------------------------------------------

	/**
	 * Get a plugin option.
	 *
	 * @param string $key     Option key (without prefix).
	 * @param mixed  $default Default value when option does not exist.
	 * @return mixed
	 */
	public function get( string $key, mixed $default = null ): mixed {
		return get_option( self::PREFIX . sanitize_key( $key ), $default );
	}

	/**
	 * Update (or create) a plugin option.
	 *
	 * @param string $key   Option key (without prefix).
	 * @param mixed  $value Value to store.
	 * @return bool True if value changed, false otherwise.
	 */
	public function set( string $key, mixed $value ): bool {
		return update_option( self::PREFIX . sanitize_key( $key ), $value );
	}

	/**
	 * Delete a plugin option.
	 *
	 * @param string $key Option key (without prefix).
	 * @return bool True on success.
	 */
	public function delete( string $key ): bool {
		return delete_option( self::PREFIX . sanitize_key( $key ) );
	}

	// ---------------------------------------------------------------------------
	// Module-scoped option API
	// ---------------------------------------------------------------------------

	/**
	 * Get an option for a specific module.
	 *
	 * Key format: stryke_wp_module_{module_id}_{key}
	 *
	 * @param string $module_id Module identifier (e.g. 'connector').
	 * @param string $key       Setting key.
	 * @param mixed  $default   Default value.
	 * @return mixed
	 */
	public function module_get( string $module_id, string $key, mixed $default = null ): mixed {
		return $this->get( "module_{$module_id}_{$key}", $default );
	}

	/**
	 * Set an option for a specific module.
	 *
	 * @param string $module_id Module identifier.
	 * @param string $key       Setting key.
	 * @param mixed  $value     Value to store.
	 * @return bool
	 */
	public function module_set( string $module_id, string $key, mixed $value ): bool {
		return $this->set( "module_{$module_id}_{$key}", $value );
	}

	// ---------------------------------------------------------------------------
	// Module enabled / disabled state
	// ---------------------------------------------------------------------------

	/**
	 * Check if a module is enabled.
	 *
	 * @param string $module_id Module identifier.
	 * @param bool   $default   Default state when option doesn't exist.
	 * @return bool
	 */
	public function is_module_enabled( string $module_id, bool $default = false ): bool {
		return (bool) $this->module_get( $module_id, 'enabled', $default );
	}

	/**
	 * Enable or disable a module.
	 *
	 * @param string $module_id Module identifier.
	 * @param bool   $enabled   Desired state.
	 * @return bool
	 */
	public function set_module_enabled( string $module_id, bool $enabled ): bool {
		return $this->module_set( $module_id, 'enabled', $enabled );
	}
}
