<?php
/**
 * WooCommerce PayPal Payments Integration
 *
 * Integrates with WooCommerce PayPal Payments plugin for warranty tracking.
 *
 * @since   1.0.0
 * @package WarrantyCart
 */

namespace WarrantyCart\Integrations;

use WC_Order;

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

/**
 * WooCommerce PayPal Payments Integration
 *
 * Integrates with WooCommerce PayPal Payments plugin for warranty tracking.
 *
 * @since 1.0.0
 */
class PayPalIntegration extends AbstractIntegration {

	/**
	 * Constructor
	 */
	public function __construct() {
		$this->plugin_slug = 'woocommerce-paypal-payments/woocommerce-paypal-payments.php';
		$this->plugin_name = 'WooCommerce PayPal Payments';
		$this->min_version = '2.0.0';

		parent::__construct();
	}

	/**
	 * Get integration ID
	 *
	 * @return string
	 */
	public function get_id(): string {
		return 'paypal';
	}

	/**
	 * Get integration name
	 *
	 * @return string
	 */
	public function get_name(): string {
		return \__( 'PayPal Payments Integration', 'warranty-cart' );
	}

	/**
	 * Get integration description
	 *
	 * @return string
	 */
	public function get_description(): string {
		return \__( 'Integrates with WooCommerce PayPal Payments for warranty tracking and order processing.', 'warranty-cart' );
	}

	/**
	 * Initialize the PayPal integration
	 *
	 * @return void
	 */
	public function init(): void {
		if ( ! $this->is_ready() ) {
			return;
		}

		$this->init_hooks();
	}

	/**
	 * Initialize PayPal-specific hooks
	 *
	 * @return void
	 */
	protected function init_hooks(): void {
		parent::init_hooks();

		// Hook into PayPal payment completion
		\add_action( 'woocommerce_order_status_completed', array( $this, 'handle_paypal_payment_completed' ), 10, 1 );

		// Hook into PayPal order status changes
		\add_action( 'woocommerce_paypal_payments_order_status_changed', array( $this, 'handle_paypal_order_status_changed' ), 10, 3 );
	}

	/**
	 * Handle PayPal payment completion
	 *
	 * @param int $order_id Order ID.
	 *
	 * @return void
	 */
	public function handle_paypal_payment_completed( int $order_id ): void {
		$order = \wc_get_order( $order_id );

		if ( ! $order instanceof WC_Order ) {
			return;
		}

		// Check if this order was paid via PayPal
		$payment_method = $order->get_payment_method();
		if ( 'ppcp-gateway' !== $payment_method && 'paypal' !== $payment_method ) {
			return;
		}

		// Process warranty items for this order
		$this->process_warranty_order( $order );
	}

	/**
	 * Handle PayPal order status changes
	 *
	 * @param string   $new_status New order status.
	 * @param string   $old_status Old order status.
	 * @param WC_Order $order      Order object.
	 *
	 * @return void
	 */
	public function handle_paypal_order_status_changed( string $new_status, string $old_status, WC_Order $order ): void {
		// Only process when order is completed
		if ( 'completed' !== $new_status ) {
			return;
		}

		$this->process_warranty_order( $order );
	}

	/**
	 * Process warranty items for a PayPal order
	 *
	 * @param WC_Order $order Order object.
	 *
	 * @return void
	 */
	protected function process_warranty_order( WC_Order $order ): void {
		// Check if order contains warranty products
		$warranty_items = $this->get_order_warranty_items( $order );

		if ( empty( $warranty_items ) ) {
			return;
		}

		// Log the warranty processing
		$this->log_warranty_processing( $order->get_id(), $warranty_items );

		// Trigger warranty-specific actions
		\do_action( 'warranty_cart_paypal_payment_completed', $order, $warranty_items );
	}

	/**
	 * Get warranty items from order
	 *
	 * @param WC_Order $order Order object.
	 *
	 * @return array
	 */
	protected function get_order_warranty_items( WC_Order $order ): array {
		$warranty_items = array();

		foreach ( $order->get_items() as $item ) {
			$product = $item->get_product();

			if ( ! $product ) {
				continue;
			}

			// Check if this is a warranty product
			if ( $product->get_meta( '_warranty_cart_product_status', true ) === 'enabled' && $product->meta_exists( '_warranty_cart_product_type' ) ) {
				$warranty_items[] = array(
					'product_id'    => $product->get_id(),
					'product_name'  => $product->get_name(),
					'quantity'      => $item->get_quantity(),
					'total'         => $item->get_total(),
					'warranty_type' => $product->get_meta( '_warranty_cart_product_type', true ),
					'duration'      => $product->get_meta( '_warranty_cart_duration', true ),
					'duration_unit' => $product->get_meta( '_warranty_cart_duration_unit', true ),
				);
			}
		}

		return $warranty_items;
	}

	/**
	 * Log warranty processing
	 *
	 * @param int   $order_id       Order ID.
	 * @param array $warranty_items Warranty items.
	 *
	 * @return void
	 */
	protected function log_warranty_processing( int $order_id, array $warranty_items ): void {
		$message = \sprintf(
			'PayPal payment completed for order #%d with %d warranty item(s)',
			$order_id,
			\count( $warranty_items )
		);

		\wc_get_logger()->info( $message, array( 'source' => 'warranty-cart-paypal' ) );
	}
}
