from __future__ import annotations

from receipts.models import BaseReceipt, compute_hash
from receipts.sign import sign_receipt


def finalize_receipt(receipt: BaseReceipt) -> BaseReceipt:
    """
    Recompute and attach the canonical hash for a receipt before returning it.
    """
    object.__setattr__(receipt, "receipt_hash", compute_hash(receipt))
    signature_block = sign_receipt(receipt)
    object.__setattr__(receipt, "signature", signature_block)
    return receipt
