<?php

/**
 * The admin-specific functionality of the plugin.
 *
 * @link       https://mediahubster.in
 * @since      1.0.0
 *
 * @package    Subs_Sys
 * @subpackage Subs_Sys/admin
 */

// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * The admin settings functionality of the plugin.
 *
 * Defines the plugin name, version, and hooks for managing settings.
 *
 * @package    Subs_Sys
 * @subpackage Subs_Sys/admin
 * @author     Joe Youngblood <info@mediahubster.in>
 */
class Subs_Sys_Settings {

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $version    The current version of this plugin.
	 */
	private $version;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 * @param    string $plugin_name       The name of this plugin.
	 * @param    string $version    The version of this plugin.
	 */
	public function __construct( $plugin_name, $version ) {
		$this->plugin_name = $plugin_name;
		$this->version     = $version;
	}

	/**
	 * Register the settings for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function register_settings() {
		// Register settings sections
		add_settings_section(
			'subscription_general_settings',
			esc_html__('General Settings', 'subscription-system'),
			array($this, 'render_general_settings_section'),
			'subscription-settings'
		);

		// Register settings fields
		add_settings_field(
			'subscription_enable_registration',
			esc_html__('Enable Registration', 'subscription-system'),
			array($this, 'render_enable_registration_field'),
			'subscription-settings',
			'subscription_general_settings'
		);

		add_settings_field(
			'subscription_enable_login',
			esc_html__('Enable Login', 'subscription-system'),
			array($this, 'render_enable_login_field'),
			'subscription-settings',
			'subscription_general_settings'
		);

		add_settings_field(
			'subscription_default_role',
			esc_html__('Default User Role', 'subscription-system'),
			array($this, 'render_default_role_field'),
			'subscription-settings',
			'subscription_general_settings'
		);

		// Register settings
		register_setting('subscription_settings', 'subscription_enable_registration');
		register_setting('subscription_settings', 'subscription_enable_login');
		register_setting('subscription_settings', 'subscription_default_role');
	}

	/**
	 * Sanitize all settings.
	 *
	 * @since    1.0.0
	 * @param    array $input    Settings input.
	 * @return   array           Sanitized settings.
	 */
	public function sanitize_all_settings( $input ) {
		$sanitized = array();

		if ( isset( $input['subs_sys_enable_registration'] ) ) {
			$sanitized['subs_sys_enable_registration'] = $this->sanitize_boolean( $input['subs_sys_enable_registration'] );
		}

		if ( isset( $input['subs_sys_enable_login'] ) ) {
			$sanitized['subs_sys_enable_login'] = $this->sanitize_boolean( $input['subs_sys_enable_login'] );
		}

		if ( isset( $input['subs_sys_default_role'] ) ) {
			$sanitized['subs_sys_default_role'] = $this->sanitize_user_role( $input['subs_sys_default_role'] );
		}

		return $sanitized;
	}

	/**
	 * General settings section callback.
	 *
	 * @since    1.0.0
	 */
	public function general_section_callback() {
		echo '<p>' . esc_html__( 'Configure the general settings for the subscription system.', 'subscription-system' ) . '</p>';
	}

	/**
	 * Enable registration field callback.
	 *
	 * @since    1.0.0
	 */
	public function enable_registration_callback() {
		$value = get_option( 'subs_sys_enable_registration', false );
		echo '<input type="checkbox" id="subs_sys_enable_registration" name="subs_sys_enable_registration" value="1" ' . checked( 1, $value, false ) . ' />';
		echo '<label for="subs_sys_enable_registration">' . esc_html__( 'Allow new user registration', 'subscription-system' ) . '</label>';
	}

	/**
	 * Enable login field callback.
	 *
	 * @since    1.0.0
	 */
	public function enable_login_callback() {
		$value = get_option( 'subs_sys_enable_login', false );
		echo '<input type="checkbox" id="subs_sys_enable_login" name="subs_sys_enable_login" value="1" ' . checked( 1, $value, false ) . ' />';
		echo '<label for="subs_sys_enable_login">' . esc_html__( 'Enable custom login functionality', 'subscription-system' ) . '</label>';
	}

	/**
	 * Default role field callback.
	 *
	 * @since    1.0.0
	 */
	public function default_role_callback() {
		$value = get_option( 'subs_sys_default_role', 'subscriber' );
		$roles = wp_roles()->get_names();

		echo '<select id="subs_sys_default_role" name="subs_sys_default_role">';
		foreach ( $roles as $role_key => $role_name ) {
			echo '<option value="' . esc_attr( $role_key ) . '" ' . selected( $role_key, $value, false ) . '>' . esc_html( $role_name ) . '</option>';
		}
		echo '</select>';
		echo '<p class="description">' . esc_html__( 'Default role for new registered users.', 'subscription-system' ) . '</p>';
	}

	/**
	 * Sanitize boolean values.
	 *
	 * @since    1.0.0
	 * @param    mixed $value The value to sanitize.
	 * @return   bool         The sanitized boolean value.
	 */
	public function sanitize_boolean( $value ) {
		return (bool) $value;
	}

	/**
	 * Sanitize user role values.
	 *
	 * @since    1.0.0
	 * @param    string $value The role to sanitize.
	 * @return   string        The sanitized role or default.
	 */
	public function sanitize_user_role( $value ) {
		$value = sanitize_text_field( $value );

		// Get available roles.
		$roles = wp_roles()->get_names();

		// Return the value if it's a valid role, otherwise return default.
		return array_key_exists( $value, $roles ) ? $value : 'subscriber';
	}
}
