<?php
/**
 * Checkout integration: render the field, validate it, save it to the order.
 *
 * Targets the classic (shortcode) checkout, which is still the most widely used.
 * The field uses a native <input type="date"> — no jQuery UI, no locale breakage.
 *
 * @package Datery
 */

defined( 'ABSPATH' ) || exit;

class Datery_Checkout {

	private static $instance = null;

	public static function instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	private function __construct() {
		add_action( 'woocommerce_after_order_notes', array( $this, 'render_field' ) );
		add_action( 'woocommerce_checkout_process', array( $this, 'validate_field' ) );
		add_action( 'woocommerce_checkout_create_order', array( $this, 'save_field' ), 10, 2 );

		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) );
	}

	public function render_field( $checkout ) {
		$required = 'yes' === Datery_Settings::get( 'required', 'yes' );
		$label    = Datery_Settings::get( 'field_label', __( 'Delivery date', 'datery-delivery-date-for-woocommerce' ) );

		echo '<div id="datery-delivery-date-wrap" class="datery-delivery-date">';
		echo '<h3>' . esc_html( $label ) . '</h3>';

		woocommerce_form_field(
			'datery_delivery_date',
			array(
				'type'              => 'date',
				'class'             => array( 'form-row-wide', 'datery-delivery-date-field' ),
				'label'             => $label,
				'required'          => $required,
				'custom_attributes' => array(
					'min' => Datery_Dates::min_date(),
					'max' => Datery_Dates::max_date(),
				),
			),
			$checkout->get_value( 'datery_delivery_date' )
		);

		echo '</div>';
	}

	public function validate_field() {
		// WooCommerce verifies the checkout nonce before woocommerce_checkout_process fires.
		$value = isset( $_POST['datery_delivery_date'] ) ? sanitize_text_field( wp_unslash( $_POST['datery_delivery_date'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing

		// Empty + not required -> fine.
		if ( '' === $value ) {
			if ( 'yes' === Datery_Settings::get( 'required', 'yes' ) ) {
				wc_add_notice( __( 'Please choose a delivery date.', 'datery-delivery-date-for-woocommerce' ), 'error' );
			}
			return;
		}

		$result = Datery_Dates::validate( $value );
		if ( is_wp_error( $result ) ) {
			wc_add_notice( $result->get_error_message(), 'error' );
		}
	}

	public function save_field( $order, $data ) {
		// WooCommerce verifies the checkout nonce before woocommerce_checkout_create_order fires.
		$value = isset( $_POST['datery_delivery_date'] ) ? sanitize_text_field( wp_unslash( $_POST['datery_delivery_date'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
		if ( '' === $value ) {
			return;
		}
		// Re-validate before persisting so we never store a bad value.
		if ( is_wp_error( Datery_Dates::validate( $value ) ) ) {
			return;
		}
		$order->update_meta_data( DATERY_META_KEY, $value );
	}

	public function enqueue_assets() {
		if ( ! function_exists( 'is_checkout' ) || ! is_checkout() ) {
			return;
		}
		wp_enqueue_style( 'datery-wc', DATERY_URL . 'assets/css/style.css', array(), DATERY_VERSION );
	}
}
