<?php
/**
 * Payment-method labels for the SIN.CORE createOrder payload.
 *
 * @package Simplyin\Simplyin_W_Plugin\Service\Order
 */

declare(strict_types=1);

namespace Simplyin\Simplyin_W_Plugin\Service\Order;

use Simplyin\Simplyin_W_Plugin\Payments\Gateway\Simplyin_Gateway;
use WC_Order;

/**
 * Builds the payment-method labels sent to SIN.CORE in the createOrder payload.
 *
 * Contract:
 * - Case A (order paid via the Simply.IN gateway): baseline `method` = "Simply.IN",
 *   `title` = ''. The concrete provider and instrument (e.g. "Simply.IN · Provider" +
 *   masked card) are appended by SIN.CORE at payment time — they are NOT
 *   read here, because on the order-created hook the payment has not been processed
 *   yet and (in block checkout) the instrument data does not exist server-side at all.
 * - Case B (order paid with a native shop gateway): `method` = the human gateway name
 *   the buyer saw at checkout (e.g. "Cash on delivery"), not the gateway slug ("cod");
 *   `title` = '' (WooCommerce exposes no payment "type" distinct from the name).
 *
 * The values are read from the order itself, so both createOrder paths (checkout hook
 * and background sync) produce identical labels for the same order.
 */
class Payment_Method_Label {

	public const SIMPLYIN_BASE_LABEL = 'Simply.IN';

	/**
	 * @param WC_Order $order Order to derive the labels from.
	 *
	 * @return array{method: string, title: string}
	 */
	public static function for_order( WC_Order $order ): array {
		if ( Simplyin_Gateway::GATEWAY_ID === $order->get_payment_method() ) {
			$method = self::SIMPLYIN_BASE_LABEL;
		} else {
			// Human gateway name persisted on the order by WooCommerce at checkout.
			// Fall back to the slug only when no title was stored (better than an
			// empty label — the slug still identifies the method).
			$method = (string) $order->get_payment_method_title();
			if ( '' === $method ) {
				$method = (string) $order->get_payment_method();
			}
		}

		return [
			'method' => $method,
			'title'  => '',
		];
	}
}
