<?php
/**
 * Helpers for filtering WooCommerce order statuses.
 *
 * @package FoodOrd
 */

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

if ( ! function_exists( 'foodord_normalize_order_status_slug' ) ) {
	/**
	 * Normalize a status slug to the canonical WooCommerce format.
	 *
	 * @param string $status Raw status slug.
	 * @return string Normalized slug.
	 */
	function foodord_normalize_order_status_slug( $status ): string {
		$normalized = strtolower( trim( (string) $status ) );
		$normalized = str_replace( array( ' ', '_', '\\' ), '-', $normalized );

		if ( '' === $normalized ) {
			return '';
		}

		if ( 0 === strpos( $normalized, 'wc-' ) ) {
			return $normalized;
		}

		if ( in_array( $normalized, array( 'trash', 'auto-draft' ), true ) ) {
			return $normalized;
		}

		return 'wc-' . $normalized;
	}
}

if ( ! function_exists( 'foodord_get_excluded_order_statuses' ) ) {
	/**
	 * Provide the list of excluded statuses (normalized).
	 *
	 * @return array<string>
	 */
	function foodord_get_excluded_order_statuses(): array {
		$base = array(
			'wc-checkout-draft',
			'checkout-draft',
			'wc-draft',
			'draft',
			'wc-auto-draft',
			'auto-draft',
		);

		$normalized = array();
		foreach ( $base as $status ) {
			$slug = foodord_normalize_order_status_slug( $status );
			if ( '' !== $slug ) {
				$normalized[] = $slug;
			}
		}

		/**
		 * Filter excluded order statuses before use.
		 *
		 * @param array<string> $normalized Excluded statuses.
		 */
		return apply_filters( 'foodord_excluded_order_statuses', array_values( array_unique( $normalized ) ) );
	}
}

if ( ! function_exists( 'foodord_get_allowed_order_statuses' ) ) {
	/**
	 * Compute allowed statuses after exclusion.
	 *
	 * @return array<string>
	 */
	function foodord_get_allowed_order_statuses(): array {
		if ( ! function_exists( 'wc_get_order_statuses' ) ) {
			return array();
		}

		$all_statuses = array_keys( wc_get_order_statuses() );
		$excluded     = foodord_get_excluded_order_statuses();

		if ( empty( $all_statuses ) ) {
			return array();
		}

		$allowed = array();
		foreach ( $all_statuses as $status ) {
			$normalized = foodord_normalize_order_status_slug( $status );
			if ( '' === $normalized ) {
				continue;
			}
			if ( in_array( $normalized, $excluded, true ) ) {
				continue;
			}
			$allowed[] = $normalized;
		}

		if ( empty( $allowed ) ) {
			return $all_statuses;
		}

		return array_values( array_unique( $allowed ) );
	}
}

if ( ! function_exists( 'foodord_prepare_status_filter' ) ) {
	/**
	 * Prepare the status filter for order queries.
	 *
	 * @param mixed $raw_status Raw status parameter from request.
	 * @return array<string>
	 */
	function foodord_prepare_status_filter( $raw_status ): array {
		$allowed = foodord_get_allowed_order_statuses();
		if ( empty( $allowed ) ) {
			return array();
		}

		$normalize_input = static function ( $status ) {
			$normalized = foodord_normalize_order_status_slug( $status );
			if ( '' === $normalized ) {
				return null;
			}
			return $normalized;
		};

		if ( empty( $raw_status ) || 'any' === $raw_status ) {
			return $allowed;
		}

		$requested = array();
		if ( is_array( $raw_status ) ) {
			$requested = $raw_status;
		} else {
			$requested = explode( ',', (string) $raw_status );
		}

		$filtered = array();
		foreach ( $requested as $status ) {
			$normalized = $normalize_input( $status );
			if ( null === $normalized ) {
				continue;
			}
			if ( in_array( $normalized, $allowed, true ) ) {
				$filtered[] = $normalized;
			}
		}

		if ( empty( $filtered ) ) {
			return $allowed;
		}

		return array_values( array_unique( $filtered ) );
	}
}

if ( ! function_exists( 'foodord_is_excluded_order' ) ) {
	/**
	 * Determine if the provided order uses an excluded status.
	 *
	 * @param WC_Order|false $order Order instance.
	 * @return bool
	 */
	function foodord_is_excluded_order( $order ): bool {
		if ( ! $order || ! is_a( $order, 'WC_Order' ) ) {
			return false;
		}

		$status      = foodord_normalize_order_status_slug( $order->get_status() );
		$excluded    = foodord_get_excluded_order_statuses();
		$is_excluded = in_array( $status, $excluded, true );

		return $is_excluded;
	}
}
