<?php
/**
 * GitHub Sign-in Module
 *
 * @package GitHubSignIn
 */

namespace GitHubSignIn;

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 = 'github-sign-in';

	private ?Provider $provider = null;

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

	public function getName(): string {
		return __( 'GitHub Sign-in', 'wppowersuite-lite' );
	}

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

	public function getDescription(): string {
		return __( 'GitHub login for your site—great for technical audiences, with optional new-user signup.', 'wppowersuite-lite' );
	}

	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['client_id'] === '' || $s['client_secret'] === '' ) {
			return;
		}
		$this->bootSocialSignInModule( ! empty( $s['woo_login'] ) );
	}

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

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

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

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