<?php
/**
 * Handles views.
 *
 * @package flo-sync
 */

namespace NMFS\Admin;

use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Sync\Orders\Models\Order_Base_Model;

defined( 'ABSPATH' ) || exit;

/**
 * Class for handling views.
 */
class Views_Admin {

	/**
	 * Setup actions/filters for class.
	 */
	public static function setup(): void {

		// Show Apps order number in admin.
		add_filter(
			'manage_shop_order_posts_columns',
			array( self::class, 'manage_shop_order_posts_columns' ),
			20
		);

		add_filter(
			'manage_shop_order_posts_custom_column',
			array( self::class, 'manage_shop_order_posts_custom_column' ),
			10,
			2
		);
	}

	/**
	 * Add title for Hub ID as column in the order list.
	 *
	 * @param array $columns Array of columns.
	 *
	 * @return array Updated array of columns.
	 */
	public static function manage_shop_order_posts_columns( array $columns ): array {
		return array_merge( $columns, array( 'apps_order_id' => 'Hub ID' ) );
	}

	/**
	 * Add content for Hub ID as column in the order list.
	 *
	 * @param string $column_key Column key.
	 * @param int    $post_id    Order post id.
	 *
	 * @return void
	 */
	public static function manage_shop_order_posts_custom_column( string $column_key, int $post_id ): void {
		if ( 'apps_order_id' === $column_key ) {
			$order_model      = new Order_Base_Model( wc_get_order( $post_id ) );
			$hub_order_number = $order_model->get_hub_order_number();

			if ( ! is_null( $hub_order_number ) ) {
				echo esc_html( '#' . $hub_order_number );
			}
		}
	}
}
