<?php

namespace WPD_Platform\Factories;

use WPD_Platform\Utils\HostInterface;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Base host class that implements the HostInterface
 *
 * Provides common functionality for both internal and external hosts,
 * including token management and host type identification.
 *
 * @package WPD_Platform\Factories
 * @since 1.0.0
 */
abstract class BaseHost implements HostInterface {

	/** @var string The authentication token */
	private $token;

	/** @var string The type of host (internal/external) */
	protected $host_type;

	/** @var string The secret key for authentication */
	private $secret = '';

	/**
	 * Constructor
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
	}

	/**
	 * Get the database token, generating a new one if it doesn't exist
	 *
	 * @since 1.0.0
	 * @return string The database token
	 */
	public function get_db_token() {
		$token = get_option( self::TOKEN_OPTION );

		if ( ! $token ) {
			$token = $this->generate_db_token();
		}

		return $token;
	}

	/**
	 * Generate a new database token
	 *
	 * @since 1.0.0
	 * @return string The generated token
	 * @throws \Exception If random bytes generation fails
	 */
	private function generate_db_token() {
		$token = bin2hex( random_bytes( 20 ) );
		update_option( self::TOKEN_OPTION, $token );

		return $token;
	}

	/**
	 * Get the authentication token
	 *
	 * @since 1.0.0
	 * @return string The authentication token
	 */
	public function get_token(): string {
		return $this->token;
	}

	/**
	 * Get the secret key
	 *
	 * @since 1.0.0
	 * @return string The secret key
	 */
	public function get_secret(): string {
		return $this->secret;
	}

	/**
	 * Set the authentication token
	 *
	 * @since 1.0.0
	 * @param string $token The token to set
	 * @return void
	 */
	protected function set_token( $token ): void {
		$this->token = $token;
	}

	/**
	 * Set the secret key
	 *
	 * @since 1.0.0
	 * @param string $secret The secret to set
	 * @return void
	 */
	protected function set_secret( $secret ): void {
		$this->secret = $secret;
	}

	/**
	 * Get the host type
	 *
	 * @since 1.0.0
	 * @return string The host type
	 */
	public function get_type() {
		return $this->host_type;
	}

	/**
	 * Register the site with the platform
	 *
	 * @since 1.0.0
	 * @return void
	 */
	public function register_site() {
	}

	/**
	 * Check if the site is connected to the platform
	 *
	 * @since 1.0.0
	 * @return bool Whether the site is connected
	 */
	public function is_connected() {
		return false;
	}

	/**
	 * Remove the site connection
	 *
	 * @since 1.0.0
	 * @return bool Whether the connection was removed successfully
	 */
	public function remove_connection() {
		return true;
	}

	/**
	 * Check if this is an internal host
	 *
	 * @since 1.0.0
	 * @return bool Whether this is an internal host
	 */
	public function is_type_internal() {
		return $this->get_type() === self::TYPE_1;
	}

	/**
	 * Check if this is an external host
	 *
	 * @since 1.0.0
	 * @return bool Whether this is an external host
	 */
	public function is_type_external() {
		return $this->get_type() === self::TYPE_2;
	}
}
