<?php

namespace Booktics\Module\Stripe_Addon\Hook;

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

use Booktics\Contracts\Hookable_Service_Contract;
use Booktics\Module\Stripe_Addon\Handler\Stripe_Handler;
use Booktics\Module\Stripe_Addon\Service\Stripe_Payment_Method;
use Booktics\Payment\Payment_Method_Manager;

/**
 * Implements WordPress hooks for Stripe payment integration.
 *
 * This class handles registration of AJAX callbacks and filters
 * for Stripe payment processing and settings management.
 *
 * @since 1.0.0
 */
class Stripe_Hook implements Hookable_Service_Contract {

	/**
	 * Register WordPress hooks for Stripe functionality.
	 *
	 * Registers AJAX callback for Stripe processing and filters
	 * for payment methods and settings.
	 *
	 * @return void
	 * @since 1.0.0
	 */
	public function register() {
		add_action( 'wp_ajax_booktics_stripe_callback', [
			Stripe_Handler::class,
			'handle_callback'
		] );

		add_filter( 'booktics_payment_methods', function ( $methods ) {
			$methods['stripe'] = Stripe_Payment_Method::class;

			return $methods;
		} );

		add_filter( 'booktics_format_setting_response', [ $this, 'stripe_settings' ] );
	}

	/**
	 * Modifies settings array to include Stripe-specific settings.
	 *
	 * Adds Stripe installation status, connection URLs for test/live modes,
	 * and account ID to the settings array.
	 *
	 * @param array $settings Existing settings array.
	 *
	 * @return array Modified settings array with Stripe data.
	 * @since 1.0.0
	 */
	public function stripe_settings( $settings ) {
		$settings['stripe_addon_install_status'] = 1;
		$stripe                                  = ( new Payment_Method_Manager() )->get_payment_method( 'stripe' );
		$settings['stripe_connect_test_url']     = $stripe::connect_account( 'test' );
		$settings['stripe_connect_live_url']     = $stripe::connect_account( 'live' );
		$settings['stripe_account_id']           = $stripe::account_id();

		return $settings;
	}
}