<?php
/**
 * Accounts Module Settings
 *
 * Registers account-specific settings in the settings page.
 *
 * @package AmbassadorHub\Accounts\Admin\Settings
 */

namespace AmbassadorHub\Accounts\Admin\Settings;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use AmbassadorHub\Admin\Settings\Fields;
use AmbassadorHub\Admin\Settings\Registration;

use function add_action;
use function checked;
use function esc_html__;
use function get_option;

/**
 * Handles registration of account-related settings.
 *
 * Registers where the Account/My Account page should be.
 */
final class SettingsManager {
	/**
	 * Initialize settings registration hooks.
	 *
	 * @return void
	 */
	public static function init(): void {

		add_action( 'ambh_register_settings', array( self::class, 'register_settings' ) );
		add_action( 'ambh_settings_advanced_page_fields', array( self::class, 'render_page_settings' ) );
	}

	public static function register_settings(): void {

		register_setting(
			'ambh_advanced',
			'ambh_account_page_id',
			array(
				'sanitize_callback' => array( Registration::class, 'sanitize_page_id' ),
				'default'           => 0,
				'type'              => 'integer',
			)
		);

		register_setting(
			'ambh_advanced',
			'ambh_login_page_id',
			array(
				'sanitize_callback' => array( Registration::class, 'sanitize_page_id' ),
				'default'           => 0,
				'type'              => 'integer',
			)
		);

		do_action( 'ambh_accounts_register_settings' );
	}

	/**
	 * Register the Account page setting.
	 *
	 * Allows admins to select which page contains the Account/My Account functionality.
	 * This page should have the Account block placed on it.
	 *
	 * @return void
	 */
	public static function render_page_settings(): void {

		Fields::page_selector(
			'ambh_account_page_id',
			esc_html__( 'Account Page', 'ambassadorhub' ),
			esc_html__( 'Select the page where the Account block (My Account) will be placed. Ambassadors will access their account information on this page.', 'ambassadorhub' )
		);

		Fields::page_selector(
			'ambh_login_page_id',
			esc_html__( 'Login / Signup Page', 'ambassadorhub' ),
			esc_html__( 'Select the page where the Login block will be placed. New ambassadors and existing users will access login and registration on this page.', 'ambassadorhub' )
		);
	}

	/**
	 * Get the configured Account page ID.
	 *
	 * @return int|false Page ID or false if not configured.
	 */
	public static function get_account_page_id() {
		$page_id = get_option( 'ambh_account_page_id', 0 );
		return $page_id ? (int) $page_id : false;
	}

	/**
	 * Check if Account page is configured.
	 *
	 * @return bool True if configured, false otherwise.
	 */
	public static function is_account_page_configured(): bool {
		return (bool) self::get_account_page_id();
	}

	/**
	 * Get the URL of the Account page, falling back to home URL.
	 *
	 * @return string
	 */
	public static function get_account_page_url(): string {
		$page_id = self::get_account_page_id();
		return $page_id ? (string) get_permalink( $page_id ) : home_url( '/' );
	}

	/**
	 * Get the configured Login page ID.
	 *
	 * @return int|false Page ID or false if not configured.
	 */
	public static function get_login_page_id() {
		$page_id = get_option( 'ambh_login_page_id', 0 );
		return $page_id ? (int) $page_id : false;
	}

	/**
	 * Check if Login page is configured.
	 *
	 * @return bool True if configured, false otherwise.
	 */
	public static function is_login_page_configured(): bool {
		return (bool) self::get_login_page_id();
	}

	/**
	 * Get the URL of the Login page, falling back to home URL.
	 *
	 * @return string
	 */
	public static function get_login_page_url(): string {
		$page_id = self::get_login_page_id();
		return $page_id ? (string) get_permalink( $page_id ) : home_url( '/' );
	}
}
