<?php
defined( 'ABSPATH' ) || exit;

add_action( 'woocommerce_before_calculate_totals', 'formulate_pricing_set_cart_price', 20 );
add_filter( 'woocommerce_get_item_data', 'formulate_pricing_cart_item_data', 10, 2 );
add_action( 'woocommerce_checkout_create_order_line_item', 'formulate_pricing_order_item_meta', 10, 4 );
add_action( 'woocommerce_checkout_order_created', 'formulate_pricing_link_order', 10, 1 );
add_action( 'woocommerce_order_status_changed', 'formulate_pricing_sync_order_status', 10, 3 );

/**
 * Override cart line price for Formulate Pricing submissions.
 *
 * @param WC_Cart $cart Cart object.
 */
function formulate_pricing_set_cart_price( $cart ) {
	if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
		return;
	}
	if ( ! $cart || empty( $cart->get_cart() ) ) {
		return;
	}

	foreach ( $cart->get_cart() as $cart_item ) {
		if ( ! isset( $cart_item['formulate_pricing']['custom_price'] ) ) {
			continue;
		}
		$cart_item['data']->set_price( (float) $cart_item['formulate_pricing']['custom_price'] );
	}
}

/**
 * Cart/checkout line item display for form field selections.
 *
 * @param array $item_data Item data rows.
 * @param array $cart_item Cart item.
 * @return array
 */
function formulate_pricing_cart_item_data( $item_data, $cart_item ) {
	if ( empty( $cart_item['formulate_pricing']['form_id'] ) || empty( $cart_item['formulate_pricing']['entry_data'] ) ) {
		return $item_data;
	}
	if ( ! class_exists( 'Formulate_Pricing_Form' ) ) {
		return $item_data;
	}

	$lines = Formulate_Pricing_Form::get_cart_display_items(
		(int) $cart_item['formulate_pricing']['form_id'],
		$cart_item['formulate_pricing']['entry_data']
	);

	foreach ( $lines as $line ) {
		$item_data[] = array(
			'key'   => $line['key'],
			'value' => esc_html( $line['value'] ),
		);
	}

	return $item_data;
}

/**
 * Persist form submission meta on the order line item.
 *
 * @param WC_Order_Item_Product $item Order line item.
 * @param string                $cart_item_key Cart item key.
 * @param array                 $values Cart item values.
 * @param WC_Order              $order Order.
 */
function formulate_pricing_order_item_meta( $item, $cart_item_key, $values, $order ) {
	if ( empty( $values['formulate_pricing'] ) ) {
		return;
	}

	$b = $values['formulate_pricing'];

	if ( ! empty( $b['form_id'] ) ) {
		$item->add_meta_data( '_formulate_pricing_form_id', (string) (int) $b['form_id'], true );
	}

	if ( ! empty( $b['entry_id'] ) ) {
		$entry_id = (int) $b['entry_id'];
		$item->add_meta_data( '_formulate_pricing_entry_id', (string) $entry_id, true );
		$item->add_meta_data( 'Entry ID', '#' . $entry_id, true );
	}

	if ( empty( $b['form_id'] ) || empty( $b['entry_data'] ) || ! class_exists( 'Formulate_Pricing_Form' ) ) {
		return;
	}

	$lines = Formulate_Pricing_Form::get_cart_display_items( (int) $b['form_id'], $b['entry_data'] );
	foreach ( $lines as $line ) {
		$item->add_meta_data( sanitize_text_field( $line['key'] ), sanitize_text_field( $line['value'] ), true );
	}
}

/**
 * Link WooCommerce order to entries and sync payment status.
 *
 * @param WC_Order $order Order object.
 */
function formulate_pricing_link_order( $order ) {
	if ( class_exists( 'Formulate_Pricing_Entry' ) ) {
		Formulate_Pricing_Entry::sync_payment_from_order( $order->get_id() );
	}
}

/**
 * Sync entry payment status when order status changes.
 *
 * @param int    $order_id Order ID.
 * @param string $old_status Old status.
 * @param string $new_status New status.
 */
function formulate_pricing_sync_order_status( $order_id, $old_status, $new_status ) {
	if ( class_exists( 'Formulate_Pricing_Entry' ) ) {
		Formulate_Pricing_Entry::sync_payment_from_order( $order_id );
	}
}
