<?php

/**
 * Receipt status helper.
 *
 * @package northmill-online-shared
 */
declare (strict_types=1);
namespace Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Sync\Receipts;

use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Abstract_Module;
/**
 * Class Receipt_Status
 *
 * @noinspection PhpUnused To be extended in packages loading this composer package.
 */
class Receipt_Status_Module extends Abstract_Module
{
    public const RECEIPT_STATUS_META_KEY = '_mf_receipt_status';
    /**
     * Get order receipt status.
     *
     * @param int $order_id Order ID.
     *
     * @return Receipt_Status_Enum|false|null Status if found, false if not found, null if invalid order ID.
     */
    public function get_order_receipt_status(int $order_id): Receipt_Status_Enum|false|null
    {
        if (!$order_id) {
            return null;
        }
        $status = get_post_meta($order_id, self::RECEIPT_STATUS_META_KEY, \true);
        $status = Receipt_Status_Enum::tryFrom($status);
        return !empty($status) ? $status : \false;
    }
    /**
     * Check if the receipt status of a given order matches the specified status.
     *
     * @param int                 $order_id The ID of the order.
     * @param Receipt_Status_Enum $status   The status to check against the order's receipt status.
     *
     * @return bool True if the order's receipt status matches the specified status, false otherwise.
     */
    public function is_order_receipt_status(int $order_id, Receipt_Status_Enum $status): bool
    {
        $current_status = $this->get_order_receipt_status($order_id);
        return $current_status === $status;
    }
    /**
     * Update order receipt status.
     *
     * @param int                 $order_id Order ID.
     * @param Receipt_Status_Enum $status   Status.
     *
     * @return bool True if the status was updated, false otherwise.
     *
     * @noinspection PhpUnused To be called by packages loading this composer package.
     */
    public function update_order_receipt_status(int $order_id, Receipt_Status_Enum $status): bool
    {
        if (!$order_id) {
            return \false;
        }
        if ($this->is_order_receipt_status($order_id, $status)) {
            return \true;
        }
        return (bool) update_post_meta($order_id, self::RECEIPT_STATUS_META_KEY, $status->value);
    }
    /**
     * Reset the receipt status for a given order if it is marked as 'receipt-failed'.
     *
     * @param int $order_id The ID of the order.
     *
     * @return bool True if the status was reset, false otherwise.
     *
     * @noinspection PhpUnused To be called by packages loading this composer package.
     */
    public function maybe_reset_receipt_status(int $order_id): bool
    {
        if (!$order_id) {
            return \false;
        }
        if ($this->is_order_receipt_status($order_id, Receipt_Status_Enum::FAILED)) {
            delete_post_meta($order_id, self::RECEIPT_STATUS_META_KEY);
            return \true;
        }
        return \false;
    }
}
