<?php
namespace EasyInvoice\Abstracts;

use EasyInvoice\Interfaces\PaymentGatewayInterface;

abstract class AbstractPaymentGateway implements PaymentGatewayInterface {
    /**
     * Gateway settings
     * 
     * @var array
     */
    protected $settings = [];

    /**
     * Constructor
     */
    public function __construct() {
        // Hook into the payment method form display
        add_action('easy_invoice_payment_method_form', [$this, 'maybeRenderForm'], 10, 2);
    }

    /**
     * Initialize the payment gateway
     */
    public function init(): void {
        $this->settings = $this->getSettings();
    }

    /**
     * Get payment gateway settings
     * 
     * @return array
     */
    public function getSettings(): array {
        $settings = [];
        
        switch ($this->getName()) {
            case 'paypal':
                $settings['email'] = get_option('easy_invoice_paypal_email', '');
                $settings['mode'] = get_option('easy_invoice_paypal_mode', 'live');
                break;
                
            // Stripe, Bank Transfer, Cheque, and Cash settings moved to Pro plugin
        }
        
        return $settings;
    }

    /**
     * Save payment gateway settings
     * 
     * @param array $settings
     * @return bool
     */
    protected function saveSettings(array $settings): bool {
        switch ($this->getName()) {
            case 'paypal':
                return update_option('easy_invoice_paypal_email', $settings['email'] ?? '');
                
            // Stripe, Bank Transfer, Cheque, and Cash settings moved to Pro plugin
                
            default:
                return false;
        }
    }

    /**
     * Check if payment gateway is enabled
     * 
     * @return bool
     */
    public function isEnabled(): bool {
        $payment_methods = get_option('easy_invoice_payment_methods', []);
        $is_enabled = in_array($this->getName(), $payment_methods);
        return $is_enabled;
    }
    
    /**
     * Log a message
     *
     * @param string $message The message to log
     * @param string $level The log level (info, warning, error)
     * @return void
     */
    protected function log($message, $level = 'info') {
        // Allow plugins to handle logging
        do_action('easy_invoice_payment_gateway_log', $message, $level, $this->getName());
    }

    /**
     * Get gateway settings configuration for the settings page
     * 
     * @return array The settings configuration array
     */
    public function getSettingsConfig(): array {
        // Base implementation returns an empty array
        // Gateway classes should override this method to provide their specific settings
        return [
            'show_header' => false,
            'header_text' => '',
            'header_class' => '',
            'fields' => []
        ];
    }

    /**
     * Render the payment form for this gateway
     * 
     * @param object $invoice The invoice object
     * @return void
     */
    public function renderPaymentForm($invoice): void {
        // Base implementation does nothing
        // Gateway classes should override this method to provide their specific payment form
    }

    /**
     * Check if this is the gateway being requested and render its form if so
     * 
     * @param string $gateway_id The gateway ID from the hook
     * @param object $invoice The invoice object
     */
    public function maybeRenderForm($gateway_id, $invoice): void {
        if ($gateway_id === $this->getName()) {
            $this->renderPaymentForm($invoice);
        }
    }
} 