<?php
/**
 * Facebook Sign In Module
 *
 * @package FacebookSignIn
 */

namespace FacebookSignIn;

use WP_PowerSuite\Core\AbstractModule;
use WP_PowerSuite\OAuth\SocialSignInModuleTrait;

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

require_once __DIR__ . '/Config.php';
require_once __DIR__ . '/Provider.php';

class Module extends AbstractModule {

	use SocialSignInModuleTrait;

	private const MODULE_ID = 'facebook-sign-in';

	private ?Provider $provider = null;

	public function getId(): string {
		return self::MODULE_ID;
	}

	public function getName(): string {
		return __( 'Facebook Sign In', 'wp-powersuite' );
	}

	public function getVersion(): string {
		return '1.1.0';
	}

	public function getDescription(): string {
		return __( 'Add “Continue with Facebook” on your login page so visitors can use their Meta account instead of another password. Works with the optional shortcode for custom layouts.', 'wp-powersuite' );
	}

	public function getContext(): string|array {
		return 'both';
	}

	private function isLicenseActive(): bool {
		return function_exists( 'wp_powersuite_is_license_valid' ) && wp_powersuite_is_license_valid();
	}

	protected function registerHooks(): void {
		if ( ! $this->isLicenseActive() ) {
			return;
		}

		$this->provider = new Provider( $this->moduleSettings() );
		$s              = $this->provider->getSettings();
		if ( $s['app_id'] === '' || $s['app_secret'] === '' ) {
			return;
		}
		$this->bootSocialSignInModule( ! empty( $s['woo_login'] ) );
	}

	protected function getSocialProviderSlug(): string {
		return 'facebook';
	}

	protected function getSocialProvider(): object {
		return $this->provider ?? new Provider( $this->moduleSettings() );
	}

	protected function getSocialLegacyShortcodeTag(): string {
		return 'facebook_sign_in';
	}

	protected function getSocialProviderSettings(): array {
		return ( $this->provider instanceof Provider ) ? $this->provider->getSettings() : [];
	}
}
