<?php
/**
 * Abstract Payment Gateway
 */

namespace RadiusTheme\RadiusBooking\Abstracts;

use RadiusTheme\RadiusBooking\Interfaces\PaymentGatewayInterface;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
} // Exit if accessed directly

/**
 * Abstract Payment Gateway
 *
 * Base class for all payment gateways with common functionality
 */
abstract class AbstractPaymentGateway implements PaymentGatewayInterface {

	/**
	 * Gateway configuration
	 *
	 * @var array Configuration
	 */
	protected $config;

	/**
	 * Logger instance
	 *
	 * @var mixed Logger instance
	 */
	protected $logger;

	/**
	 * Constructor
	 *
	 * @param array $config Gateway configuration
	 */
	public function __construct( array $config = array() ) {
		$this->config = $config;
		$this->logger = $this->getLogger();
	}

	/**
	 * Log payment activity
	 *
	 * @param string $level Log level (info, error, warning)
	 * @param string $message Log message
	 * @param array $context Additional context
	 */
	protected function log( string $level, string $message, array $context = array() ) {
		if ( $this->logger ) {
			$this->logger->log( $level, $message, $context );
		}
		// Fallback to error_log if no logger available
		error_log(
			sprintf(
				'[%s] %s: %s %s',
				strtoupper( $this->getGatewayName() ),
				strtoupper( $level ),
				$message,
				! empty( $context ) ? wp_json_encode( $context ) : ''
			)
		);
	}

	/**
	 * Get logger instance
	 *
	 * @return mixed Logger instance or null
	 */
	protected function getLogger() {
		// Override this to use your logging system
		return null;
	}

	/**
	 * Validate required payment data fields
	 *
	 * @param array $paymentData Payment data to validate
	 * @param array $requiredFields List of required fields
	 *
	 * @return array Array with 'valid' boolean and 'errors' array
	 */
	protected function validatePaymentData( array $paymentData, array $requiredFields ): array {
		$errors = array();

		foreach ( $requiredFields as $field ) {
			if ( ! isset( $paymentData[ $field ] ) || empty( $paymentData[ $field ] ) ) {
				$errors[] = sprintf( 'Missing required field: %s', $field );
			}
		}

		return array(
			'valid'  => empty( $errors ),
			'errors' => $errors,
		);
	}

	/**
	 * Create standard response array.
	 *
	 * Adds common metadata such as gateway name and timestamp.
	 *
	 * @param bool   $success Whether the operation was successful.
	 * @param string $message Response message.
	 * @param array  $data    Additional data to merge into the response.
	 *
	 * @return array Associative response array.
	 */
	protected function createResponse( bool $success, string $message, array $data = array() ): array {
		return array_merge(
			array(
				'success'   => $success,
				'message'   => $message,
				'gateway'   => $this->getGatewayName(),
				'timestamp' => current_time( 'mysql' ),
			),
			$data
		);
	}

	/**
	 * Get configuration value.
	 *
	 * @param string $key     Configuration key.
	 * @param mixed|null $defaultData Default value if the key is not present.
	 *
	 * @return mixed Configuration value or $default when not set.
	 */
	protected function getConfig( string $key, mixed $defaultData = null ) {
		return $this->config[ $key ] ?? $defaultData;
	}
}
