<?php
/**
 * Settings payment method model.
 *
 * @package northmill-online-shared
 */

declare(strict_types=1);

namespace Northmill_Online_Shared\Modules\Admin\Pages\Page\Admin_Sync_Settings\Models\Properties;

use InvalidArgumentException;
use Northmill_Online_Shared\Data\Payment_Methods;

defined( 'ABSPATH' ) || exit;

/**
 * Class Settings_Payment_Method_Model
 *
 * @used Northmill Express
 */
readonly class Settings_Payment_Method_Model {

	/**
	 * WC Gateway ID.
	 *
	 * @var string
	 */
	private string $gateway_id;

	/**
	 * Payment method type in Hub.
	 *
	 * @var string
	 */
	private string $hub_type;


	/**
	 * Constructor.
	 *
	 * @param bool        $is_enabled           If the payment method is enabled in Hub.
	 * @param string      $settings_title       Payment method settings title.
	 * @param string      $settings_description Payment method settings description.
	 * @param string      $settings_username    Payment method settings username.
	 * @param string      $settings_password    Payment method settings password.
	 * @param string      $settings_key         Payment method settings key.
	 * @param string|null $hub_type             Payment method type in Hub. This or gateway_id must be set.
	 * @param string|null $gateway_id           WC Gateway ID.
	 *
	 * @throws InvalidArgumentException If neither hub_type nor gateway_id is set, or is unrecognized.
	 */
	public function __construct(
		private bool $is_enabled,
		private string $settings_title,
		private string $settings_description,
		private string $settings_username,
		private string $settings_password,
		private string $settings_key,
		?string $hub_type = null,
		?string $gateway_id = null,
	) {
		if ( is_null( $hub_type ) && is_null( $gateway_id ) ) {
			throw new InvalidArgumentException( 'Either hub_type or gateway_id must be set.' );
		}

		if ( is_null( $hub_type ) ) {
			$this->gateway_id = $gateway_id;
			$this->set_hub_type( $gateway_id );
		} elseif ( is_null( $gateway_id ) ) {
			$this->hub_type = $hub_type;
			$this->set_gateway_id( $hub_type );
		}
	}

	/**
	 * Set the hub type.
	 *
	 * @param string $gateway_id WC Gateway ID.
	 *
	 * @throws InvalidArgumentException If hub_type could not be determined from gateway_id.
	 */
	private function set_hub_type( string $gateway_id ): void {
		$hub_type = Payment_Methods::get_online_solution_type( $gateway_id );
		if ( is_null( $hub_type ) ) {
			throw new InvalidArgumentException( 'hub_type could not be determined from gateway_id.' );
		}
		$this->hub_type = $hub_type;
	}

	/**
	 * Set the gateway ID.
	 *
	 * @param string $hub_type Payment method type in Hub.
	 *
	 * @throws InvalidArgumentException If gateway_id could not be determined from hub_type.
	 */
	private function set_gateway_id( string $hub_type ): void {
		$gateway_id = Payment_Methods::get_gateway_id_from_online_solution_type( $hub_type );

		if ( is_null( $gateway_id ) ) {
			throw new InvalidArgumentException( 'gateway_id could not be determined from hub_type.' );
		}

		$this->gateway_id = $gateway_id;
	}

	/**
	 * Get whether the payment method is enabled.
	 *
	 * @return bool
	 */
	public function is_enabled(): bool {
		return true === $this->is_enabled;
	}

	/**
	 * Get the static payment method title.
	 *
	 * @return string
	 */
	public function get_name(): string {
		return Payment_Methods::get_method_title( $this->gateway_id );
	}

	/**
	 * Get the payment method type.
	 *
	 * @return string
	 */
	public function get_type(): string {
		return $this->hub_type;
	}

	/**
	 * Get the payment method title synced from Hub to gateway options.
	 *
	 * @return string
	 */
	public function get_settings_title(): string {
		return $this->settings_title;
	}

	/**
	 * Get the payment method description synced from Hub to gateway options.
	 *
	 * @return string
	 */
	public function get_settings_description(): string {
		return $this->settings_description ?? '';
	}

	/**
	 * Get the payment method username.
	 *
	 * @return string
	 */
	public function get_settings_username(): string {
		return $this->settings_username;
	}

	/**
	 * Get the payment method password.
	 *
	 * @return string
	 */
	public function get_settings_password(): string {
		return $this->settings_password;
	}

	/**
	 * Get the payment method key.
	 *
	 * @return string
	 */
	public function get_settings_key(): string {
		return $this->settings_key;
	}
}
