<?php

namespace DigitalApps\PaymentsForAdyen;

defined('ABSPATH') || exit;

/**
 * Admin notices for Adyen payment gateway issues
 */
class Adyen_Admin_Notices
{
	public function __construct()
	{
		add_action('admin_notices', [$this, 'show_credential_warnings']);
	}

	/**
	 * Show admin notices for missing credentials or configuration issues
	 */
	public function show_credential_warnings()
	{
		// Only show to users who can manage WooCommerce.
		if (!current_user_can('manage_woocommerce')) {
			return;
		}

		// Check if any Adyen gateways are enabled
		$adyen_gateways_enabled = $this->check_enabled_adyen_gateways();

		if (empty($adyen_gateways_enabled)) {
			return; // No Adyen gateways enabled, no need to show warnings
		}

		// Check if credentials are missing
		$settings = get_option('digitalapps_payments_for_adyen_settings', []);
		$missing_credentials = empty($settings['apiKey']) || empty($settings['merchantAccount']);

		if ($missing_credentials) {
			$this->show_missing_credentials_notice($adyen_gateways_enabled);
		}
	}

	/**
	 * Check which Adyen gateways are enabled
	 */
	private function check_enabled_adyen_gateways()
	{
		$enabled_gateways = [];

		$adyen_gateways = [
			'digitalapps_payments_for_adyen_card' => 'Adyen Card',
			'digitalapps_payments_for_adyen_googlepay' => 'Google Pay',
			'digitalapps_payments_for_adyen_paypal' => 'PayPal',
			'digitalapps_payments_for_adyen_ideal' => 'iDEAL',
			'digitalapps_payments_for_adyen_sepa' => 'SEPA',
			'digitalapps_payments_for_adyen_sofort' => 'Sofort',
			'digitalapps_payments_for_adyen_bcmc' => 'Bancontact',
			'digitalapps_payments_for_adyen_klarna' => 'Klarna',
			'digitalapps_payments_for_adyen_afterpay_au' => 'Afterpay',
			'digitalapps_payments_for_adyen_alipay' => 'Alipay',
			'digitalapps_payments_for_adyen_wechatpay' => 'WeChat Pay',
			'digitalapps_payments_for_adyen_pix' => 'PIX'
		];

		foreach ($adyen_gateways as $gateway_id => $gateway_name) {
			$gateway_settings = get_option("woocommerce_{$gateway_id}_settings", []);
			if (!empty($gateway_settings['enabled']) && $gateway_settings['enabled'] === 'yes') {
				$enabled_gateways[] = $gateway_name;
			}
		}

		return $enabled_gateways;
	}

	/**
	 * Show notice for missing credentials
	 */
	private function show_missing_credentials_notice($enabled_gateways)
	{
		$gateway_list = implode(', ', $enabled_gateways);

		echo '<div class="notice notice-error is-dismissible">';
		echo '<h3>🚨 Adyen Payment Methods Not Working</h3>';
		echo '<p><strong>Issue:</strong> You have enabled the following Adyen payment methods but <strong>API credentials are missing</strong>:</p>';
		echo '<p><strong>' . esc_html($gateway_list) . '</strong></p>';
		echo '<p><strong>Impact:</strong> Customers will see "There are no payment methods available" at checkout.</p>';
		echo '<p><strong>Action Required:</strong></p>';
		echo '<ol>';
		echo '<li>Get your API credentials from your <a href="https://ca-test.adyen.com/" target="_blank">Adyen Customer Area</a></li>';
		echo '<li>Go to <a href="' . esc_url(admin_url('admin.php?page=wc-settings&tab=checkout')) . '">WooCommerce → Settings → Payments</a></li>';
		echo '<li>Configure your Adyen API Key and Merchant Account</li>';
		echo '</ol>';
		echo '<p><em>This notice will disappear once you configure your Adyen credentials.</em></p>';
		echo '</div>';
	}
}

// Initialize the admin notices
new Adyen_Admin_Notices();
