<?php
/**
 * Payment gateway settings and event gateway availability.
 *
 * @package PlugozaEventTicketing
 */

namespace PlugozaEventTicketing;

defined( 'ABSPATH' ) || exit;

final class Payment_Gateways {
	const OPTION = 'plugoza_ett_payment_gateways';

	public function __construct() {
		add_action( 'admin_init', array( $this, 'register_settings' ) );
	}

	public static function defaults() {
		return array(
			'bank_transfer' => array(
				'enabled'      => false,
				'bank_name'    => '',
				'account_name' => '',
				'account_number' => '',
				'ifsc_swift'   => '',
				'branch'       => '',
				'upi_id'       => '',
				'instructions' => '',
			),
			'free_registration' => array(
				'enabled'      => true,
				'instructions' => '',
			),
		);
	}

	public function register_settings() {
		register_setting( 'plugoza_ett_payment_gateways', self::OPTION, array( $this, 'sanitize_settings' ) );
	}

	public function sanitize_settings( $input ) {
		$input    = is_array( $input ) ? $input : array();
		$settings = self::defaults();
		$bank     = isset( $input['bank_transfer'] ) && is_array( $input['bank_transfer'] ) ? $input['bank_transfer'] : array();

		$settings['bank_transfer']['enabled']        = ! empty( $bank['enabled'] );
		$settings['bank_transfer']['bank_name']      = sanitize_text_field( wp_unslash( $bank['bank_name'] ?? '' ) );
		$settings['bank_transfer']['account_name']   = sanitize_text_field( wp_unslash( $bank['account_name'] ?? '' ) );
		$settings['bank_transfer']['account_number'] = sanitize_text_field( wp_unslash( $bank['account_number'] ?? '' ) );
		$settings['bank_transfer']['ifsc_swift']     = sanitize_text_field( wp_unslash( $bank['ifsc_swift'] ?? '' ) );
		$settings['bank_transfer']['branch']         = sanitize_text_field( wp_unslash( $bank['branch'] ?? '' ) );
		$settings['bank_transfer']['upi_id']         = sanitize_text_field( wp_unslash( $bank['upi_id'] ?? '' ) );
		$settings['bank_transfer']['instructions']   = sanitize_textarea_field( wp_unslash( $bank['instructions'] ?? '' ) );
		$manual_pay = isset( $input['free_registration'] ) && is_array( $input['free_registration'] ) ? $input['free_registration'] : array();
		$settings['free_registration']['enabled']      = ! empty( $manual_pay['enabled'] );
		$settings['free_registration']['instructions'] = sanitize_textarea_field( wp_unslash( $manual_pay['instructions'] ?? '' ) );

		return $settings;
	}

	public static function settings() {
		$stored = get_option( self::OPTION, array() );
		return array_replace_recursive( self::defaults(), is_array( $stored ) ? $stored : array() );
	}

	public static function event_methods( $event ) {
		$methods = get_option( 'plugoza_ett_event_payment_methods_' . absint( $event->id ), array() );
		return is_array( $methods ) ? array_values( array_intersect( $methods, array( 'bank_transfer', 'free_registration' ) ) ) : array();
	}

	public static function save_event_methods( $event_id, $methods ) {
		$methods = is_array( $methods ) ? $methods : array();
		update_option( 'plugoza_ett_event_payment_methods_' . absint( $event_id ), array_values( array_intersect( $methods, array( 'bank_transfer', 'free_registration' ) ) ), false );
	}

	public static function event_legal_links( $event_id ) {
		$links = get_option( 'plugoza_ett_event_legal_links_' . absint( $event_id ), array() );
		return wp_parse_args( is_array( $links ) ? $links : array(), array( 'terms' => '', 'privacy' => '', 'refund' => '' ) );
	}

	public static function save_event_legal_links( $event_id, $links ) {
		$links = is_array( $links ) ? $links : array();
		update_option( 'plugoza_ett_event_legal_links_' . absint( $event_id ), array( 'terms' => esc_url_raw( $links['terms'] ?? '' ), 'privacy' => esc_url_raw( $links['privacy'] ?? '' ), 'refund' => esc_url_raw( $links['refund'] ?? '' ) ), false );
	}

	public static function available_methods( $event ) {
		$settings = self::settings();
		$methods  = self::event_methods( $event );
		return array_values(
			array_filter(
				$methods,
				static function ( $method ) use ( $settings ) {
					return ! empty( $settings[ $method ]['enabled'] );
				}
			)
		);
	}
}
