<?php
/**
 * Freefactu Checkout Fields
 *
 * Handles the addition of NIF/CIF fields to WooCommerce checkout and admin areas.
 *
 * @package Freefactu
 * @since 0.5.0
 */

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

/**
 * Class Freefactu_Checkout_Fields
 */
class Freefactu_Checkout_Fields {

	/**
	 * Initialize hooks.
	 */
	public static function init() {
		// Only add hooks if the option is enabled
		$options = get_option( 'freefactu_settings' );
		if ( empty( $options['enable_nif_field'] ) ) {
			return;
		}

		// Add field to checkout
		add_filter( 'woocommerce_checkout_fields', array( __CLASS__, 'add_checkout_field' ) );

		// Add field to admin order edit (Input)
		add_filter( 'woocommerce_admin_billing_fields', array( __CLASS__, 'add_admin_billing_field' ) );

		// Add field to admin order display (Display text)
		add_action( 'woocommerce_admin_order_data_after_billing_address', array( __CLASS__, 'add_admin_display_field' ), 10, 1 );

		// Save field from admin order edit - NOT NEEDED if using woocommerce_admin_billing_fields?
		// WC automatically saves fields defined in woocommerce_admin_billing_fields if they match _billing_{key}
		// Let's keep it just in case or verify. Usually WC handles it.
		// Actually, WC handles saving of standard fields. Custom fields might need help?
		// WC_Meta_Box_Order_Data::save() loops through config fields. So it should save automatically.
		// We can remove save_admin_field if we use the filter correctly.
	}

	/**
	 * Add NIF field to checkout billing section.
	 *
	 * @param array $fields Checkout fields.
	 * @return array Modified fields.
	 */
	public static function add_checkout_field( $fields ) {
		$fields['billing']['billing_nif'] = array(
			'type'        => 'text',
			'label'       => __( 'NIF/CIF/NIE', 'freefactu' ),
			'placeholder' => __( 'Introduce tu NIF/CIF/NIE', 'freefactu' ),
			'required'    => false,
			'class'       => array( 'form-row-wide' ),
			'priority'    => 35,
		);

		return $fields;
	}

	/**
	 * Add NIF field to admin billing fields (Edit Form).
	 *
	 * @param array $fields Billing fields.
	 * @return array Modified billing fields.
	 */
	public static function add_admin_billing_field( $fields ) {
		$new_fields = array();
		
		foreach ( $fields as $key => $field ) {
			$new_fields[ $key ] = $field;
			
			// Insert after company
			if ( $key === 'company' ) {
				$new_fields['nif'] = array(
					'label' => __( 'NIF/CIF/NIE', 'freefactu' ),
					'show'  => false, // Don't show in the formatted address (we handle display separately)
				);
			}
		}
		
		// Fallback if company field not found
		if ( ! isset( $new_fields['nif'] ) ) {
			$new_fields['nif'] = array(
				'label' => __( 'NIF/CIF/NIE', 'freefactu' ),
				'show'  => false,
			);
		}

		return $new_fields;
	}

	/**
	 * Add NIF field to admin order display (Display Text).
	 *
	 * @param WC_Order $order Order object.
	 */
	public static function add_admin_display_field( $order ) {
		$nif = $order->get_meta( '_billing_nif' );
		?>
		<div class="freefactu-nif-display">
			<p>
				<strong><?php esc_html_e( 'NIF/CIF/NIE', 'freefactu' ); ?>:</strong>
				<?php echo esc_html( $nif ); ?>
			</p>
		</div>
		<?php
		// Note: The JavaScript to move this field into the address block
		// is now in assets/js/freefactu-admin.js (NIF DISPLAY FIELD POSITION HANDLER)
	}
}
