<?php
namespace PFTF;

use Dompdf\Dompdf;

class Utils {

    // 🧾 Generar PDF del pedido
    public static function generate_order_pdf( $order ) {
        require_once __DIR__ . '/../vendor/autoload.php';

        $order_id = $order->get_id();
        $dompdf = new Dompdf();

        $html = "<h1>Resumen de Pedido #{$order_id}</h1>";
        $html .= "<p>Total: " . $order->get_formatted_order_total() . "</p>";
        $html .= "<p>Método de pago: " . $order->get_payment_method_title() . "</p>";

        $dompdf->loadHtml($html);
        $dompdf->setPaper('A4', 'portrait');
        $dompdf->render();

        $upload_dir = wp_upload_dir();
        $pdf_path = $upload_dir['basedir'] . "/pedido_{$order_id}.pdf";
        file_put_contents($pdf_path, $dompdf->output());

        return $pdf_path;
    }

    // 📄 Renderizar instrucciones en HTML
public static function render_instructions_html( $order_id, $instructions ) {
    $order = wc_get_order( $order_id );
    if ( ! $order ) return '';
    
    $data = get_post_meta( $order_id, '_pagos_fiable_response', true );

    $pdf_url = wp_upload_dir()['baseurl'] . "/pedido_{$order_id}.pdf";

    $customer_name  = $order->get_formatted_billing_full_name();
    $customer_email = $order->get_billing_email();
    $order_total    = wc_price( $order->get_total() );
    $order_date     = $order->get_date_created()->date('d/m/Y H:i');

    ob_start();
    ?>

    <div class="card border-secondary mb-4">
<div class="card-header bg-light d-flex justify-content-between align-items-center">
    <h5 class="mb-0">Transferencia bancaria</h5>

         <img src="<?php echo esc_url( plugins_url('assets/images/logo_pf.jpg', dirname(__FILE__)) ); ?>" alt="">

</div>

        <div class="card-body">
<p><?php echo esc_html( wptexturize( $instructions ) ); ?></p>

            <table class="table table-bordered table-sm mb-4">
                <tbody>
                    <tr><th>ID del pedido</th><td><?php echo esc_html( $order_id ); ?></td></tr>
                    <tr><th>Cliente</th><td><?php echo esc_html( $customer_name ); ?> (<?php echo esc_html( $customer_email ); ?>)</td></tr>
                    <tr><th>Fecha</th><td><?php echo esc_html( $order_date ); ?></td></tr>
                    <tr><th>Total</th><td><?php echo  esc_html($order_total) ; ?></td></tr>
                </tbody>
            </table>

            <h6> Productos comprados</h6>
            <div class="table-responsive mb-4">
                <table class="table table-striped table-sm">
                    <thead>
                        <tr>
                            <th>Producto</th>
                            <th>Cantidad</th>
                            <th>Total</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ( $order->get_items() as $item ) : ?>
                            <tr>
                                <td><?php echo esc_html( $item->get_name() ); ?></td>
                                <td><?php echo esc_html( $item->get_quantity() ); ?></td>
<td><?php echo esc_html( wc_price( $item->get_total() ) ); ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>

            <h6> REFERENCIA DE PAGO</h6>
            <table class="table table-bordered table-sm">
                <tbody>
                    <tr><th>BANCO</th><td>STP</td></tr>
                    <tr><th>CLABE</th><td><?php echo esc_html( $data['response']['bankAccount'] ?? 'N/A' ); ?></td></tr>
                    <tr><th>Referencia</th><td><?php echo esc_html( $data['response']['move_id'] ?? 'N/A' ); ?></td></tr>
                </tbody>
            </table>

        </div>
    </div>
    <?php
    return ob_get_clean();
}

    // 💰 Formatear moneda
    public static function format_currency( $amount, $currency = 'MXN' ) {
        return wc_price( $amount, [ 'currency' => $currency ] );
    }

    // 🔒 Sanitizar referencia
    public static function sanitize_reference( $order_id ) {
        return 'REF-' . preg_replace( '/[^0-9]/', '', $order_id );
    }
}
