from decimal import Decimal, ROUND_HALF_UP


def calculate_total(unit_price: str, quantity: int, tax_rate: str) -> str:
    if quantity <= 0:
        raise ValueError("quantity must be positive")
    price = Decimal(unit_price)
    tax = Decimal(tax_rate)
    total = price * quantity * (Decimal("1") + tax)
    return str(total.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP))


class MemoryPriceStore:
    def __init__(self):
        self.rows = []
    def save(self, quote_id: str, total: str):
        row = {"quote_id": quote_id, "total": total}
        self.rows.append(row)
        return row


class PricingService:
    def __init__(self, store):
        self.store = store
    def quote(self, quote_id: str, unit_price: str, quantity: int, tax_rate: str):
        total = calculate_total(unit_price, quantity, tax_rate)
        stored = self.store.save(quote_id, total)
        return {"quote_id": quote_id, "total": total, "stored": stored}


def api_quote(request: dict, service: PricingService):
    return service.quote(request["quote_id"], request["unit_price"], request["quantity"], request["tax_rate"])
