"""Public-page observations and conditional arithmetic, checked 2026-10-24.

No live scraper, accounts, personal bills or network. Run with Python 3.
"""
from decimal import Decimal
import json

CHECKED = '2026-10-24'
OBSERVATIONS = [
    {'id':'apple_icloud_50gb', 'service':'iCloud+ 50 GB', 'currency':'USD',
     'market_context':'Apple US public page; not an individual checkout',
     'displayed_monthly':'0.99', 'billing_description':'monthly public price',
     'annual_invoice':None,'tax_status':'not_verified',
     'cancellation':'Downgrade/cancellation takes effect after the current subscription billing period ends. Check stored content against the reduced quota.',
     'refund_entitlement':None,
     'sources':['https://www.apple.com/icloud/','https://support.apple.com/en-us/108318']},
    {'id':'google_one_basic', 'service':'Google One Basic 100 GB', 'currency':'USD',
     'market_context':'USD displayed in public text; billing country not verified',
     'displayed_monthly':'1.99','billing_description':'public /mo display; annual invoice not verified',
     'annual_invoice':None,'tax_status':'not_verified',
     'cancellation':'Can cancel membership; purchased storage lasts for the subscription term. Refund and cancellation route depend on region and purchase channel; App Store purchases use Apple.',
     'refund_entitlement':None,
     'sources':['https://one.google.com/about/plans','https://support.google.com/googleone/answer/9056360?hl=en']},
    {'id':'dropbox_plus', 'service':'Dropbox Plus 2 TB', 'currency':'USD',
     'market_context':'Public browser USD view; billing country not verified',
     'displayed_monthly':'11.99','yearly_selection_monthly_equivalent':'9.99',
     'billing_description':'CUA observed yearly radio selected at 9.99/mo, monthly radio selected at 11.99/mo',
     'annual_invoice':None,'tax_status':'not_verified',
     'cancellation':'Individual plan changes to Basic at the end of the current monthly/yearly billing cycle. App-store purchases have a different route; refund exceptions depend on region and purchase channel.',
     'refund_entitlement':None,
     'sources':['https://www.dropbox.com/plans','https://help.dropbox.com/plans/downgrade-dropbox-individual-plans','https://help.dropbox.com/plans/refund?fallback=true']},
    {'id':'adobe_acrobat_pro_jp', 'service':'Acrobat Pro individual, annual billed monthly', 'currency':'JPY',
     'market_context':'Adobe Japan public page',
     'displayed_monthly':'2530','billing_description':'annual plan paid monthly; listed price includes tax',
     'annual_invoice':None,'tax_status':'included_in_displayed_price',
     'cancellation':'Public pricing page states a fee of half the remaining annual commitment for cancellation after 14 days.',
     'refund_entitlement':None,
     'sources':['https://www.adobe.com/jp/acrobat/pricing.html']},
]


def money(value):
    if value is None:
        raise ValueError('Unknown amount is not zero')
    amount=Decimal(str(value))
    if not amount.is_finite() or amount < 0:
        raise ValueError('Expected a finite, nonnegative amount')
    return amount


def times(value, count):
    if isinstance(count,bool) or not isinstance(count,int) or count < 1:
        raise ValueError('Period count must be a positive integer')
    return format(money(value)*count,'.2f')


def complete_total(base, tax, fees):
    if tax is None or fees is None:
        return {'status':'incomplete','total':None,'reason':'Tax/fees not verified; a base subtotal is not the final invoice.'}
    return {'status':'complete_for_supplied_values','total':format(money(base)+money(tax)+money(fees),'.2f')}


def same_currency_difference(a,currency_a,b,currency_b):
    if currency_a != currency_b:
        raise ValueError('Do not subtract different currencies without an explicit exchange model')
    return format(money(a)-money(b),'.2f')


def run():
    by_id={o['id']:o for o in OBSERVATIONS}
    apple=times(by_id['apple_icloud_50gb']['displayed_monthly'],12)
    google=times(by_id['google_one_basic']['displayed_monthly'],12)
    dropbox=by_id['dropbox_plus']
    monthly=times(dropbox['displayed_monthly'],12)
    annual_display=times(dropbox['yearly_selection_monthly_equivalent'],12)
    difference=same_currency_difference(monthly,'USD',annual_display,'USD')
    adobe=money(by_id['adobe_acrobat_pro_jp']['displayed_monthly'])
    paid=adobe*4; remaining=adobe*8; fee=remaining*Decimal('0.5')
    assert (apple,google,monthly,annual_display,difference)==('11.88','23.88','143.88','119.88','24.00')
    assert (adobe*12,paid,remaining,fee,paid+fee)==tuple(map(Decimal,['30360','10120','20240','10120','20240']))
    assert complete_total(monthly,None,'0')['total'] is None
    assert complete_total(monthly,'0',None)['total'] is None
    assert complete_total(monthly,'0','0')['total']=='143.88'
    rejected=[]
    for name,call in [('unknown_amount',lambda:money(None)),('negative_amount',lambda:money('-1')),
                      ('nonfinite_amount',lambda:money('NaN')),('fractional_period',lambda:times('1',1.5)),
                      ('mixed_currency',lambda:same_currency_difference('1','USD','1','JPY'))]:
        try:call()
        except ValueError:rejected.append({'case':name,'result':'rejected_as_expected'})
        else:raise AssertionError('Invalid input accepted: '+name)
    return {'checked_on':CHECKED,'observations':OBSERVATIONS,
            'scope':'Public factual observations; no account-specific quote, refund entitlement or purchase. Currency rows are not comparable products.',
            'models':{
                'unchanged_monthly_12_payments':{'apple_usd':apple,'google_usd':google,'assumption':'12 payments at the observed monthly rate, unchanged; no verified annual offer or tax total'},
                'dropbox_display_basis_usd':{'monthly_12_payments':monthly,'yearly_display_times_12':annual_display,'difference':difference,'assumption':'Displayed equivalent is exact, rates unchanged, same tax/fees. Actual annual invoice not observed. This is not a refund or realized-saving claim.'},
                'adobe_jpy_after_four_payments':{'full_12_payments':format(adobe*12,'.0f'),'paid_four':format(paid,'.0f'),'remaining_eight':format(remaining,'.0f'),'half_remaining_fee':format(fee,'.0f'),'paid_plus_fee':format(paid+fee,'.0f'),'assumption':'Unchanged published 2530 JPY annual-monthly rate, cancellation after four full payments and after 14 days, eight remaining, published half-remaining rule; no other adjustments. Not an account-specific cancellation quote.'},
                'unknown_tax_gate':complete_total(monthly,None,'0')},
            'invalid_inputs':rejected}


if __name__=='__main__':
    print(json.dumps(run(),ensure_ascii=False,indent=2))
