<?php

namespace SugarCalendarBookings\Payments\Gateways\Onsite;

use Exception;
use SugarCalendarBookings\Appointments\AppointmentService;
use SugarCalendarBookings\Payments\Gateways\AbstractPaymentProcessor;
use SugarCalendarBookings\Payments\PaymentModel;
use SugarCalendarBookings\Plugin;

/**
 * Class OnSitePaymentProcessor.
 *
 * @since 1.0.0
 */
class OnSitePaymentProcessor extends AbstractPaymentProcessor {

	/**
	 * Authorize payment without capturing.
	 *
	 * @since 1.0.0
	 *
	 * @param PaymentModel $payment            Payment model instance.
	 * @param array        $authorization_data Authorization data.
	 *
	 * @return array Authorization data.
	 *
	 * @throws Exception If payment cannot be authorized.
	 */
	public function authorize_payment( $payment, $authorization_data ) {

		// Finalize appointment after payment processing.
		$appointment = $payment->appointments[0];

		Plugin::get( AppointmentService::class )->finalize_after_payment( $appointment );

		return $authorization_data;
	}

	/**
	 * Capture previously authorized payment.
	 *
	 * @since 1.0.0
	 *
	 * @param PaymentModel $payment      Payment model instance.
	 * @param array        $capture_data Payment data.
	 *
	 * @return array Capture data.
	 *
	 * @throws Exception If payment cannot be captured.
	 */
	public function capture_payment( $payment, $capture_data ) {

		return $capture_data;
	}

	/**
	 * Refund a completed payment.
	 *
	 * On-site payments cannot be refunded through the payment gateway API.
	 * Returns an empty array to indicate no API call is needed.
	 *
	 * @since 1.1.0
	 *
	 * @param PaymentModel $payment Payment model instance.
	 *
	 * @return array Empty array indicating no gateway API refund is needed.
	 */
	public function refund_payment( $payment ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found

		return [];
	}
}
