<?php

namespace BWDK\Support;

class WooCompatibility
{

	public static function get_store_currency(): string
	{
		$cur = get_option('woocommerce_currency');
		return is_string($cur) ? strtoupper(trim($cur)) : 'IRR';
	}

	public static function is_store_rial(): bool
	{
		return self::get_store_currency() === 'IRR';
	}

	public static function is_store_toman(): bool
	{
		$c = self::get_store_currency();
		return ($c === 'IRT' || $c === 'TOMAN');
	}

	/**
	 * Normalize a Woo amount (float major unit) to Toman (major unit).
	 * - IRR (Rial): divide by 10
	 * - IRT/TOMAN: keep
	 * - Else: keep
	 */
	public static function normalize_to_toman(float $amount): float
	{
		if (self::is_store_rial()) {
			return $amount / 10.0;
		}
		return $amount;
	}

	/**
	 * Convert any Woo amount to the BWDK unit (Toman).
	 * Input: native Woo currency (IRR or IRT)
	 * Output: Toman for Digify
	 */
	public static function convert_to_BWDK_currency(float $amount): float
	{
		$amount = (float) $amount;
		$currency = get_option('woocommerce_currency'); // e.g. IRR / IRT
		if (strtoupper($currency) === 'IRR') {
			return round($amount / 10, 2);
		}
		return round($amount, 2);
	}

	/**
	 * Convert a BWDK amount (Toman from Digify) back to WooCommerce store currency.
	 * Input: Toman from Digify
	 * Output: native Woo currency
	 */
	public static function convert_from_BWDK_currency(float $amount): float
	{
		$amount = (float) $amount;
		$currency = get_option('woocommerce_currency');
		if (strtoupper($currency) === 'IRR') {
			return round($amount * 10, 2);
		}
		return round($amount, 2);
	}

	/**
	 * Convert a BWDK amount (stored as Toman integer) back to WooCommerce store currency.
	 * @deprecated Use convert_from_BWDK_currency() instead
	 */
	public static function convert_to_store_currency(float $amount): float
	{
		return self::convert_from_BWDK_currency($amount);
	}
}
