<?php
/**
 * Plugin Name: Pagos Fiable-Transferencia Bancaria
 * Plugin URI:  https://vip.pagosfiable.com/
 * Description: Método de pago por tranferencias bancarias para WooCommerce.
 * Version:     1.0.3
 * Author:      Pagos Fiable
 * License:     GPLv2 or later
 * Text Domain: pagosfiable-transfer
 * WC requires at least: 4.0
 * WC tested up to: 8.0
 * Tested up to: 6.9
 */

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

define( 'PFTF_PATH', plugin_dir_path( __FILE__ ) );

// ── Inicialización ────────────────────────────────────────────────
add_action( 'plugins_loaded', 'pftf_init_gateway', 10 );

function pftf_init_gateway() {

    // Asegurarse de que WooCommerce esté activo
    if ( ! class_exists( 'WooCommerce' ) ) {
        add_action( 'admin_notices', 'pftf_admin_notice_wc_required' );
        return;
    }

    // Cargar clases
    require_once PFTF_PATH . 'includes/class-pagostf-wc.php';
    //require_once PFTF_PATH . 'includes/helpers.php';

    // Registrar gateway
    add_filter( 'woocommerce_payment_gateways', function( $methods ) {
        $methods[] = 'PFTF_WC_Transfer_Pagos_Fiable';
        return $methods;
    } );

    // Declarar compatibilidad HPOS
    add_action( 'before_woocommerce_init', function() {
        if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
            \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility(
                'custom_order_tables',
                __FILE__,
                true
            );
        }
    } );
}

// ── Notificación si WooCommerce no está activo ────────────────────
function pftf_admin_notice_wc_required() {
    echo '<div class="error"><p><strong>Pagos Fiable Transferencia Bancaria</strong> requiere que WooCommerce esté activo.</p></div>';
}

// ── Link de configuración en la lista de plugins ─────────────────
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), function( $links ) {
    $settings_link = '<a href="admin.php?page=wc-settings&tab=checkout&section=pagos_fiable_tf">' .
        __( 'Configuración', 'pagosfiable-transfer' ) . '</a>';
    array_unshift( $links, $settings_link );
    return $links;
} );
