<?php
/**
 * Global social account / linking policy. Stored under OptionStore key `social-accounts` (optional).
 * When keys are missing, defaults match product defaults: linking on, “email must match to connect” on,
 * and Woo “Connected accounts” menu on. The UI is always active when a social sign-in module is enabled.
 *
 * @package WP_PowerSuite\OAuth\Accounts
 */

namespace WP_PowerSuite\OAuth\Accounts;

use WP_PowerSuite\Core\OptionStoreModuleSettingsRepository;
use WP_PowerSuite\Core\OptionStorePluginSettingsRepository;

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

final class AccountLinkSettings {

	public const MODULE_ID = 'social-accounts';

	/**
	 * @return array{
	 *   linking_enabled: bool,
	 *   email_on_login: string,
	 *   require_email_match_to_connect: bool,
	 *   show_on_my_account: bool
	 * }
	 */
	public static function get(): array {
		$raw = OptionStoreModuleSettingsRepository::forModule( self::MODULE_ID )->all( [] );
		$email_on_login = sanitize_key( (string) ( $raw['email_on_login'] ?? 'auto' ) );
		if ( ! in_array( $email_on_login, [ 'auto', 'manual' ], true ) ) {
			$email_on_login = 'auto';
		}
		return [
			'linking_enabled'                => ! array_key_exists( 'linking_enabled', $raw ) || ! empty( $raw['linking_enabled'] ),
			'email_on_login'                 => $email_on_login,
			'require_email_match_to_connect' => ! array_key_exists( 'require_email_match_to_connect', $raw ) || ! empty( $raw['require_email_match_to_connect'] ),
			'show_on_my_account'             => ! array_key_exists( 'show_on_my_account', $raw ) || ! empty( $raw['show_on_my_account'] ),
		];
	}

	public static function isLinkingEnabled(): bool {
		return (bool) self::get()['linking_enabled'];
	}

	/**
	 * On first-time login, if email matches an existing WP user: auto-link vs require password + My Account.
	 */
	public static function autoLinkOnEmailMatch(): bool {
		return self::get()['email_on_login'] === 'auto';
	}

	public static function requireEmailMatchForConnect(): bool {
		return (bool) self::get()['require_email_match_to_connect'];
	}

	/**
	 * Persist global linking policy from a social sign-in module settings save.
	 *
	 * @param array<string, mixed> $raw
	 */
	public static function saveFromModuleSettings( array $raw ): void {
		if ( $raw === [] ) {
			return;
		}
		$email_on = sanitize_key( (string) ( $raw['email_on_login'] ?? 'auto' ) );
		if ( ! in_array( $email_on, [ 'auto', 'manual' ], true ) ) {
			$email_on = 'auto';
		}
		$policy = [
			'linking_enabled'                => ! empty( $raw['linking_enabled'] ),
			'email_on_login'                 => $email_on,
			'require_email_match_to_connect' => ! empty( $raw['require_email_match_to_connect'] ),
			'show_on_my_account'             => ! empty( $raw['show_on_my_account'] ),
		];
		OptionStoreModuleSettingsRepository::forModule( self::MODULE_ID )->save( $policy );

		if ( array_key_exists( 'oauth_debug', $raw ) ) {
			OptionStorePluginSettingsRepository::instance()->set( 'social_oauth_debug', ! empty( $raw['oauth_debug'] ) );
		}
	}
}
