<?php
/**
 * Manual Payment Gateway
 *
 * @package EasyInvoice
 * @since 1.0.0
 */

namespace EasyInvoice\Gateways;

use EasyInvoice\Abstracts\AbstractPaymentGateway;
use EasyInvoice\Interfaces\PaymentGatewayInterface;

/**
 * Manual Payment Gateway Class
 *
 * Handles manual payment processing for cash, check, and bank transfer payments
 *
 * @since 1.0.0
 */
class ManualGateway extends AbstractPaymentGateway implements PaymentGatewayInterface {

    /**
     * Gateway name
     *
     * @since 1.0.0
     * @var string
     */
    protected $name = 'manual';

    /**
     * Gateway title
     *
     * @since 1.0.0
     * @var string
     */
    protected $title = 'Manual Payment';

    /**
     * Gateway description
     *
     * @since 1.0.0
     * @var string
     */
    protected $description = 'Pay manually via cash, check, or bank transfer';

    /**
     * Gateway icon
     *
     * @since 1.0.0
     * @var string
     */
    protected $icon = 'fas fa-money-bill-wave';

    /**
     * Constructor
     *
     * @since 1.0.0
     */
    public function __construct() {
        $this->init();
    }

    /**
     * Initialize the gateway
     *
     * @since 1.0.0
     * @return void
     */
    public function init(): void {
        // Add any initialization logic here
    }

    /**
     * Get gateway name
     *
     * @since 1.0.0
     * @return string
     */
    public function getName(): string {
        return $this->name;
    }

    /**
     * Get gateway title
     *
     * @since 1.0.0
     * @return string
     */
    public function getTitle(): string {
        return $this->title;
    }

    /**
     * Get gateway description
     *
     * @since 1.0.0
     * @return string
     */
    public function getDescription(): string {
        return $this->description;
    }

    /**
     * Get gateway icon
     *
     * @since 1.0.0
     * @return string
     */
    public function getIcon(): string {
        return $this->icon;
    }

    /**
     * Check if gateway is available
     *
     * @since 1.0.0
     * @return bool
     */
    public function isAvailable(): bool {
        return true; // Manual payment is always available
    }

    /**
     * Process payment
     *
     * @since 1.0.0
     * @param float $amount Payment amount
     * @param array $data Additional payment data
     * @return array
     */
    public function processPayment(float $amount, array $data = []): array {
        // Manual payments are processed by admin
        return [
            'success' => true,
            'message' => __('Manual payment submitted for verification', 'easy-invoice'),
            'data' => [
                'status' => 'pending',
                'requires_verification' => true
            ]
        ];
    }

    /**
     * Handle callback
     *
     * @since 1.0.0
     * @param array $data Callback data
     * @return array
     */
    public function handleCallback(array $data): array {
        // Manual payments don't have callbacks
        return [
            'success' => true,
            'message' => __('Manual payment processed', 'easy-invoice')
        ];
    }

    /**
     * Get payment form fields
     *
     * @since 1.0.0
     * @param int $invoice_id Invoice ID
     * @return string
     */
    public function getPaymentForm(int $invoice_id): string {
        ob_start();
        $this->renderPaymentForm((object)['id' => $invoice_id]);
        return ob_get_clean();
    }

    /**
     * Render payment form
     *
     * @since 1.0.0
     * @param object $invoice Invoice object
     * @return void
     */
    public function renderPaymentForm($invoice): void {
        // Get invoice ID from invoice object
        $invoice_id = is_object($invoice) && method_exists($invoice, 'getId') ? $invoice->getId() : 0;
        ?>
        <div class="manual-payment-form">
            <div class="payment-methods">
                <div class="payment-method-option">
                    <input type="radio" id="payment-cash" name="payment_type" value="cash" checked>
                    <label for="payment-cash">
                        <i class="fas fa-money-bill-wave"></i>
                        <span><?php _e('Cash', 'easy-invoice'); ?></span>
                    </label>
                </div>
                <div class="payment-method-option">
                    <input type="radio" id="payment-bank" name="payment_type" value="bank">
                    <label for="payment-bank">
                        <i class="fas fa-university"></i>
                        <span><?php _e('Bank Transfer', 'easy-invoice'); ?></span>
                    </label>
                </div>
                <div class="payment-method-option">
                    <input type="radio" id="payment-cheque" name="payment_type" value="cheque">
                    <label for="payment-cheque">
                        <i class="fas fa-money-check"></i>
                        <span><?php _e('Cheque', 'easy-invoice'); ?></span>
                    </label>
                </div>
            </div>

            <div class="payment-details">
                <div class="cash-details" id="cash-details">
                    <p><?php _e('Please pay in cash and notify the administrator.', 'easy-invoice'); ?></p>
                </div>

                <div class="bank-details" id="bank-details" style="display: none;">
                    <?php $this->renderBankDetails(); ?>
                </div>

                <div class="cheque-details" id="cheque-details" style="display: none;">
                    <?php $this->renderChequeDetails(); ?>
                </div>
            </div>

            <div class="payment-proof-section">
                <h4><?php _e('Upload Payment Proof', 'easy-invoice'); ?></h4>
                <div class="form-group">
                    <label for="payment_proof"><?php _e('Upload receipt or proof of payment:', 'easy-invoice'); ?></label>
                    <input type="file" id="payment_proof" name="payment_proof" accept="image/*,.pdf">
                </div>
                <div class="form-group">
                    <label for="payment_notes"><?php _e('Additional notes:', 'easy-invoice'); ?></label>
                    <textarea id="payment_notes" name="payment_notes" rows="3"></textarea>
                </div>
            </div>
            
            <div class="form-actions">
                <button type="button" class="manual-payment-submit" data-invoice-id="<?php echo esc_attr($invoice_id); ?>">
                    <?php _e('Submit Manual Payment', 'easy-invoice'); ?>
                </button>
            </div>
        </div>
        <?php
    }

    /**
     * Render bank details
     *
     * @since 1.0.0
     * @return void
     */
    private function renderBankDetails(): void {
        $bank_name = get_option('easy_invoice_manual_bank_name', '');
        $account_name = get_option('easy_invoice_manual_account_name', '');
        $account_number = get_option('easy_invoice_manual_account_number', '');
        $routing_number = get_option('easy_invoice_manual_routing_number', '');
        $swift_code = get_option('easy_invoice_manual_swift_code', '');
        $bank_address = get_option('easy_invoice_manual_bank_address', '');

        if (empty($bank_name)) {
            echo '<p>' . __('Bank transfer details will be provided by the administrator.', 'easy-invoice') . '</p>';
            return;
        }

        echo '<table class="bank-details-table">';
        if ($bank_name) {
            echo '<tr><th>' . __('Bank Name', 'easy-invoice') . '</th><td>' . esc_html($bank_name) . '</td></tr>';
        }
        if ($account_name) {
            echo '<tr><th>' . __('Account Name', 'easy-invoice') . '</th><td>' . esc_html($account_name) . '</td></tr>';
        }
        if ($account_number) {
            echo '<tr><th>' . __('Account Number', 'easy-invoice') . '</th><td>' . esc_html($account_number) . '</td></tr>';
        }
        if ($routing_number) {
            echo '<tr><th>' . __('Routing Number', 'easy-invoice') . '</th><td>' . esc_html($routing_number) . '</td></tr>';
        }
        if ($swift_code) {
            echo '<tr><th>' . __('SWIFT Code', 'easy-invoice') . '</th><td>' . esc_html($swift_code) . '</td></tr>';
        }
        if ($bank_address) {
            echo '<tr><th>' . __('Bank Address', 'easy-invoice') . '</th><td>' . esc_html($bank_address) . '</td></tr>';
        }
        echo '</table>';
    }

    /**
     * Render cheque details
     *
     * @since 1.0.0
     * @return void
     */
    private function renderChequeDetails(): void {
        $payee_name = get_option('easy_invoice_manual_payee_name', '');
        $payee_address = get_option('easy_invoice_manual_payee_address', '');

        if (empty($payee_name)) {
            echo '<p>' . __('Cheque payment details will be provided by the administrator.', 'easy-invoice') . '</p>';
            return;
        }

        echo '<div class="cheque-info">';
        if ($payee_name) {
            echo '<p><strong>' . __('Make cheque payable to:', 'easy-invoice') . '</strong> ' . esc_html($payee_name) . '</p>';
        }
        if ($payee_address) {
            echo '<p><strong>' . __('Mail cheque to:', 'easy-invoice') . '</strong> ' . esc_html($payee_address) . '</p>';
        }
        echo '</div>';
    }

    /**
     * Get gateway settings
     *
     * @since 1.0.0
     * @return array
     */
    public function getSettings(): array {
        return [
            'enabled' => [
                'title' => __('Enable Manual Payment', 'easy-invoice'),
                'type' => 'checkbox',
                'description' => __('Enable manual payment option', 'easy-invoice'),
                'default' => 'yes'
            ],
            'title' => [
                'title' => __('Title', 'easy-invoice'),
                'type' => 'text',
                'description' => __('Payment method title that will be shown to customers', 'easy-invoice'),
                'default' => __('Manual Payment', 'easy-invoice')
            ],
            'description' => [
                'title' => __('Description', 'easy-invoice'),
                'type' => 'textarea',
                'description' => __('Payment method description that will be shown to customers', 'easy-invoice'),
                'default' => __('Pay manually via cash, check, or bank transfer', 'easy-invoice')
            ],
            'bank_name' => [
                'title' => __('Bank Name', 'easy-invoice'),
                'type' => 'text',
                'description' => __('Bank name for bank transfer payments', 'easy-invoice'),
                'default' => ''
            ],
            'account_name' => [
                'title' => __('Account Name', 'easy-invoice'),
                'type' => 'text',
                'description' => __('Account holder name', 'easy-invoice'),
                'default' => ''
            ],
            'account_number' => [
                'title' => __('Account Number', 'easy-invoice'),
                'type' => 'text',
                'description' => __('Bank account number', 'easy-invoice'),
                'default' => ''
            ],
            'routing_number' => [
                'title' => __('Routing Number', 'easy-invoice'),
                'type' => 'text',
                'description' => __('Bank routing number', 'easy-invoice'),
                'default' => ''
            ],
            'swift_code' => [
                'title' => __('SWIFT Code', 'easy-invoice'),
                'type' => 'text',
                'description' => __('Bank SWIFT code for international transfers', 'easy-invoice'),
                'default' => ''
            ],
            'bank_address' => [
                'title' => __('Bank Address', 'easy-invoice'),
                'type' => 'textarea',
                'description' => __('Bank address for payments', 'easy-invoice'),
                'default' => ''
            ],
            'payee_name' => [
                'title' => __('Payee Name', 'easy-invoice'),
                'type' => 'text',
                'description' => __('Name to make cheques payable to', 'easy-invoice'),
                'default' => ''
            ],
            'payee_address' => [
                'title' => __('Payee Address', 'easy-invoice'),
                'type' => 'textarea',
                'description' => __('Address to mail cheques to', 'easy-invoice'),
                'default' => ''
            ]
        ];
    }

    /**
     * Save gateway settings
     *
     * @since 1.0.0
     * @param array $settings Settings to save
     * @return bool
     */
    public function saveSettings(array $settings): bool {
        foreach ($settings as $key => $value) {
            update_option('easy_invoice_manual_' . $key, $value);
        }
        return true;
    }

    /**
     * Get gateway settings values
     *
     * @since 1.0.0
     * @return array
     */
    public function getSettingsValues(): array {
        return [
            'enabled' => get_option('easy_invoice_manual_enabled', 'yes'),
            'title' => get_option('easy_invoice_manual_title', __('Manual Payment', 'easy-invoice')),
            'description' => get_option('easy_invoice_manual_description', __('Pay manually via cash, check, or bank transfer', 'easy-invoice')),
            'bank_name' => get_option('easy_invoice_manual_bank_name', ''),
            'account_name' => get_option('easy_invoice_manual_account_name', ''),
            'account_number' => get_option('easy_invoice_manual_account_number', ''),
            'routing_number' => get_option('easy_invoice_manual_routing_number', ''),
            'swift_code' => get_option('easy_invoice_manual_swift_code', ''),
            'bank_address' => get_option('easy_invoice_manual_bank_address', ''),
            'payee_name' => get_option('easy_invoice_manual_payee_name', ''),
            'payee_address' => get_option('easy_invoice_manual_payee_address', '')
        ];
    }
}
