<?php
namespace LivraisonAPI;

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

class WooCommerce {
    
    public function __construct() {
        add_filter( 'woocommerce_checkout_fields', array( $this, 'custom_checkout_fields' ), 10, 1 );
        add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'save_checkout_fields' ), 10, 2 );
        add_action( 'woocommerce_admin_order_data_after_billing_address', array( $this, 'display_admin_order_meta' ), 10, 1 );
    }
    
    public function custom_checkout_fields( $fields ) {
        // Remove default shipping fields
        if ( isset( $fields['shipping'] ) ) {
            unset( $fields['shipping'] );
        }
        
        return $fields;
    }
    
    public function save_checkout_fields( $order_id, $posted ) {
        // ✅ CORRECTION: Vérification du nonce avec wp_unslash() et sanitize_text_field()
        if ( ! isset( $_POST['woocommerce-process-checkout-nonce'] ) || 
             ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['woocommerce-process-checkout-nonce'] ) ), 'woocommerce-process_checkout' ) ) {
            return;
        }
        
        $order = wc_get_order( $order_id );
        
        // ✅ CORRECTION: Ajout de wp_unslash()
        if ( isset( $_POST['shipping_full_name'] ) ) {
            $order->update_meta_data( '_shipping_full_name', sanitize_text_field( wp_unslash( $_POST['shipping_full_name'] ) ) );
        }
        if ( isset( $_POST['shipping_phone'] ) ) {
            $order->update_meta_data( '_shipping_phone', sanitize_text_field( wp_unslash( $_POST['shipping_phone'] ) ) );
        }
        if ( isset( $_POST['shipping_address'] ) ) {
            $order->update_meta_data( '_shipping_address', sanitize_text_field( wp_unslash( $_POST['shipping_address'] ) ) );
        }
        if ( isset( $_POST['shipping_instructions'] ) ) {
            $order->update_meta_data( '_shipping_instructions', sanitize_textarea_field( wp_unslash( $_POST['shipping_instructions'] ) ) );
        }
        if ( isset( $_POST['livraison_commune'] ) ) {
            $order->update_meta_data( '_livraison_commune', sanitize_text_field( wp_unslash( $_POST['livraison_commune'] ) ) );
        }
        
        $order->save();
    }
    
    public function display_admin_order_meta( $order ) {
        $full_name = $order->get_meta( '_shipping_full_name' );
        $phone = $order->get_meta( '_shipping_phone' );
        $address = $order->get_meta( '_shipping_address' );
        $commune = $order->get_meta( '_livraison_commune' );
        $instructions = $order->get_meta( '_shipping_instructions' );
        
        if ( ! empty( $full_name ) || ! empty( $phone ) || ! empty( $address ) ) {
            echo '<div style="margin-top: 15px; padding-top: 15px; border-top: 1px solid #ddd;">';
            echo '<h4 style="margin: 0 0 10px 0;">' . esc_html__( 'Delivery Information', 'sendi-api' ) . '</h4>';
            if ( ! empty( $full_name ) ) {
                echo '<p><strong>' . esc_html__( 'Full Name:', 'sendi-api' ) . '</strong> ' . esc_html( $full_name ) . '</p>';
            }
            if ( ! empty( $phone ) ) {
                echo '<p><strong>' . esc_html__( 'Phone:', 'sendi-api' ) . '</strong> ' . esc_html( $phone ) . '</p>';
            }
            if ( ! empty( $address ) ) {
                echo '<p><strong>' . esc_html__( 'Address:', 'sendi-api' ) . '</strong> ' . esc_html( $address ) . '</p>';
            }
            if ( ! empty( $commune ) ) {
                echo '<p><strong>' . esc_html__( 'Commune:', 'sendi-api' ) . '</strong> ' . esc_html( $commune ) . '</p>';
            }
            if ( ! empty( $instructions ) ) {
                echo '<p><strong>' . esc_html__( 'Instructions:', 'sendi-api' ) . '</strong> ' . esc_html( $instructions ) . '</p>';
            }
            echo '</div>';
        }
    }
}