<?php

namespace GOSP\GravityOps\Core\SuiteCore;

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

/**
 * Centralized configuration values for GravityOps core.
 */
class Config {

	/**
	 * Default configuration values.
	 *
	 * @var array<string,mixed>
	 */
	private array $defaults = [
		'join_community_url'         => 'https://brightleafdigital.io/plugintomember',
		'affiliate_url'              => 'https://brightleafdigital.io/affiliate/',
		'review_endpoint'            => 'https://brightleafdigital.io/wp-json/bld-freemius-backend/v1/get_review_link',
		'survey_endpoint'            => 'https://brightleafdigital.io/wp-json/gf/v2/forms/31/submissions',
		'coupon_codes'               => [
			'integrate-asana-with-gravity-forms'          => 'GRAVITYASANA10GIFT',
			'mass_email_notifications_for_gf'             => 'MASSEMAIL10GIFT',
			'Recurring_Form_Submissions_For_Gravity_Form' => 'RECURSUB10GIFT',
			'gravity_ops_global_variables'                => 'GLOBVAR10GIFT',
			'kanban-view-for-gravity-view'                => 'KANBANVIEW10GIFT',
			'gravityops-forum'                            => 'FORUM10GIFT',
			'gravityops-charts'                           => 'CHARTS10GIFT',
			'gravityops-multi-media'                      => 'MULTIMED10GIFT',
			'gravityops-live-field-validation'            => 'LIVEVAL10GIFT',
			'gravityops-multi-path-step'                  => 'MULTIPATH10GIFT',
			'gravityops-workflow-step-runner'             => 'WORKFLOW10GIFT',
			'gravityops-ai-feed-for-gravity-forms'        => 'AIFEED10GIFT',
			'gravityops-search'                           => 'SEARCH10GIFT',
		],
		'trustedlogin_api_key'       => 'd7818caaf4075548',
		'trustedlogin_webhook_url'   => 'https://brightleafdigital.io/wp-json/trustedlogin/v1/webhook',
		'trustedlogin_vendor'        => [],
		'load_admin_assets_globally' => true,
		'style_freemius_screens'     => true,
	];

	/**
	 * Config constructor.
	 */
	public function __construct() {
		$state = &SuiteCore::config_state();
		if ( empty( $state ) ) {
			$state = $this->defaults;
		}
	}

	/**
	 * Merge configuration overrides.
	 *
	 * @param array<string,mixed> $overrides Config overrides.
	 * @return void
	 */
	public function apply_overrides( array $overrides ) {
		$state = &SuiteCore::config_state();
		foreach ( $overrides as $key => $value ) {
			if ( null === $value ) {
				continue;
			}
			$state[ $key ] = $value;
		}
	}

	/**
	 * Retrieves the URL for joining the community from the stored values or defaults.
	 *
	 * @return string The community join URL as a string. Returns the default URL if no value is set.
	 */
	public function get_join_community_url() {
		return (string) $this->get_value( 'join_community_url', $this->defaults['join_community_url'] );
	}

	/**
	 * Retrieves the affiliate URL from the stored values or defaults.
	 *
	 * @return string The affiliate URL as a string. Defaults to an empty string if no value is found.
	 */
	public function get_affiliate_url() {
		return (string) $this->get_value( 'affiliate_url', $this->defaults['affiliate_url'] );
	}

	/**
	 * Retrieves the review endpoint from the stored values or defaults.
	 *
	 * @return string The review endpoint as a string. Defaults to the provided default value if not found.
	 */
	public function get_review_endpoint() {
		return (string) $this->get_value( 'review_endpoint', $this->defaults['review_endpoint'] );
	}

	/**
	 * Retrieves the survey endpoint from the stored values or defaults.
	 *
	 * @return string The survey endpoint as a string. Returns the default value if not set.
	 */
	public function get_survey_endpoint() {
		return (string) $this->get_value( 'survey_endpoint', $this->defaults['survey_endpoint'] );
	}

	/**
	 * Retrieves the coupon codes from the stored values or defaults.
	 *
	 * @return array An array of coupon codes. Returns an empty array if no valid array is found.
	 */
	public function get_coupon_codes() {
		$value = $this->get_value( 'coupon_codes', $this->defaults['coupon_codes'] );
		return is_array( $value ) ? $value : [];
	}

	/**
	 * Retrieves the TrustedLogin API key from the stored values or defaults.
	 *
	 * @return string The TrustedLogin API key as a string. Returns an empty string if no key is found.
	 */
	public function get_trustedlogin_api_key() {
		return (string) $this->get_value( 'trustedlogin_api_key', $this->defaults['trustedlogin_api_key'] );
	}

	/**
	 * Retrieves the TrustedLogin webhook URL from the stored values or defaults.
	 *
	 * @return string The TrustedLogin webhook URL. Returns an empty string if no value is found.
	 */
	public function get_trustedlogin_webhook_url() {
		return (string) $this->get_value( 'trustedlogin_webhook_url', $this->defaults['trustedlogin_webhook_url'] );
	}

	/**
	 * Retrieves the TrustedLogin vendor information from the stored values or defaults.
	 *
	 * @return array An array containing the TrustedLogin vendor details. Returns an empty array if no valid array is found.
	 */
	public function get_trustedlogin_vendor() {
		$value = $this->get_value( 'trustedlogin_vendor', $this->defaults['trustedlogin_vendor'] );
		return is_array( $value ) ? $value : [];
	}

	/**
	 * Determines whether admin assets should be loaded globally.
	 *
	 * @return bool True if admin assets are set to load globally, false otherwise.
	 */
	public function load_admin_assets_globally() {
		return (bool) $this->get_value( 'load_admin_assets_globally', $this->defaults['load_admin_assets_globally'] );
	}

	/**
	 * Determines whether Freemius screens should be styled based on stored values or defaults.
	 *
	 * @return bool True if Freemius screens should be styled, otherwise false.
	 */
	public function style_freemius_screens() {
		return (bool) $this->get_value( 'style_freemius_screens', $this->defaults['style_freemius_screens'] );
	}

	/**
	 * Fetch a config value from shared state.
	 *
	 * @param string $key Config key.
	 * @param mixed  $default_value Default value.
	 *
	 * @return mixed
	 */
	private function get_value( string $key, $default_value ) {
		$state = SuiteCore::config_state();
		return $state[ $key ] ?? $default_value;
	}
}
