<?php

namespace Booktics\Payment;

use Booktics\Payment\Contracts\Payment_Method_Interface;
use WP_Error;

/**
 * Manager for payment methods.
 *
 * Allows registering and retrieving payment methods. Also handles processing
 * payments using a specified payment method.
 *
 * @package Booktics\Core\Payment
 * @since 1.0.0
 */
class Payment_Method_Manager {

    private $payment_methods = array();

    public function __construct() {
        $this->payment_methods = apply_filters( 'booktics_payment_methods', array() );
    }

    /**
     * Get all registered payment methods
     *
     * @return array
     */
    public function get_payment_methods(): array {
        return apply_filters( 'booktics_payment_methods', $this->payment_methods );
    }

    /**
     * Get a payment method by its ID
     *
     * @param string $id
     *
     * @return Payment_Method_Interface|null Returns the payment method instance or null if not found
     */
    public function get_payment_method( string $id ): ?Payment_Method_Interface {
        $payment_methods = $this->get_payment_methods();
        if ( ! isset( $payment_methods[ $id ] ) ) {
            return null;
        }

        return new $payment_methods[ $id ]();
    }
}
