"""Pricing desk: reproducible pre-pricing estimates, not a prediction of the IPO price."""
from decimal import Decimal as D
import json
from pathlib import Path

HERE = Path(__file__).resolve().parent
ANT='https://www.sec.gov/Archives/edgar/data/2130357/000162828026072484/anthropicpbc-sx1ax2.htm'
NS='https://www.sec.gov/Archives/edgar/data/2110365/000119312526458722/ck0002110365-20261112.htm'
G='https://www.sec.gov/Archives/edgar/data/2130357/000162828026072484/exhibit1015-sx1ax2.htm'
PLACEMENT='https://www.sec.gov/Archives/edgar/data/2130357/000162828026072484/exhibit1016-sx1ax2.htm'
shares=801351354
sellers=23648646
names={'Google':10,'NVIDIA':10,'AMD':2.5,'Broadcom':1,'Micron':1,'Microsoft':1,'TSMC Partners':0.5}
def cash(price):
    x=D(str(price)); delta=x-74
    gross=lambda n: x*n/10**9
    net=D('84.6')+delta*D('.793')
    tax=D('3.8')+delta*D('.052')
    return dict(price=x,public_issuer_gross=gross(shares),selling_holder_gross=gross(sellers),
                public_total_gross=gross(shares+sellers),strategic_placement_gross=26,
                combined_net_approx=net,rsu_remittance_approx=tax,
                new_cash_after_expected_remittance_approx=net-tax,
                class_N_indicative_shares_m_before_rounding=D('26000')/x)

def amd(earned,draws):
    earned,draws=map(lambda v:D(str(v)),(earned,draws))
    if not (0<=earned<=5 and 0<=draws<=max(D(0),earned-D('2.5'))):raise ValueError('Check cumulative earned amount and prior incremental draws')
    return max(D(0),earned-D('2.5')-draws)
def timing(advance,discount_rate,months):
    # bridge carry before fees for one tranche with a single repayment at maturity
    return D(str(advance))*D(str(discount_rate))*D(str(months))/12

if __name__=='__main__':
    rows=[cash(p) for p in (70,74,78)]
    assert shares+sellers==825000000 and sum(names.values())==26
    assert [r['new_cash_after_expected_remittance_approx'] for r in rows]==list(map(D,('77.836','80.8','83.764')))
    assert [amd(x,0) for x in ('1','2.5','3.5','5')]==[0,0,1,D('2.5')] and amd('4','0.5')==1
    assert timing(1,'.08',6)==D('.04')
    exact_delta=D('212.684')-D('131.845'); assert exact_delta==D('80.839')
    assert D('5.053')+D('.121')+D('.057')==D('5.231')
    data={'status':'Scenario, not settled. Evidence checked 2026-11-13 before US market open.',
          'units':'USD billions except labeled shares and price', 'sec_issuer':'2130357',
          'sources':{'anthropic':ANT,'google_stock_contract':G,'placement_form':PLACEMENT,'nscale':NS},
          'offering':{'issuer_primary_class_A_shares':shares,'secondary_class_A_shares':sellers,'underwriter_extra_primary_option_shares':123750000,
          'no_option_in_price_cases':True,'placement_by_buyer':names,'estimated_net_in_full_option_midpoint_case':93.7,
          'before_underwriter_option_and_with_full_dollar_placement':rows,'actual_proceeds':None,
          'per_1_USD_price_change':{'issuer_combined_net_B':0.793,'expected_RSU_payment_B':0.052,'our_after_remittance_B':0.741},
          'september_capitalization_table':{'cash_equivalents_and_STI_actual':131.845,'pro_forma_as_adjusted':212.684,'midpoint_simulated_cash_delta':exact_delta},
          'precision':'Use-of-proceeds estimates rounded; capitalisation cash row is in millions. Local sensitivity is of proceeds less remittance, not issuer guidance for the pro forma balance.'},
          'carryover':{'google_original_September_NPA':20,'google_placement':10,'google_remaining_after_exercise':10,
                       'amd_original_conditional_equity':5,'amd_placement':2.5,'amd_ceiling_left_after_close':2.5,
                       'identified_old_commitment_dollars_in_placement':12.5,'share_of_placement':D('12.5')/26,
                       'amd_future_draw_examples':[{ 'cumulative_milestone_dollars':str(x),'prior_incremental_draws':0,
                          'available_incremental_draw':amd(x,0)} for x in ('1','2.5','3.5','5')],
                       'timing':'Google option exercised in filing; AMD reduction takes effect at placement closing. Other buyers not presumed new or recycled.'},
          'cash_calendar':{'as_of':'2026-09-30','scheduled_2026_purchase':5.053,'scheduled_2026_finance_leases':0.121,
                           'scheduled_2026_operating_leases':0.057,'scheduled_2026_total':5.231,
                           'broader_12m_total_to_2027_09_30':70.7,'broader_12m_conditional_subset':4.2,
                           'cash_assigned_to_individual_named_project':None},
          'nscale':{'date':'2026-11-12','max_Anthropic_services_total_across_four_tranches':44.6,
                    'building1_power_and_facility_total_capex_range':[10,10.5],'facility_incurred_through_sep30_approx':1,
                    'facility_remaining_range':[9,9.5],'campus_compute_equipment_total_capex_range':[21,22],
                    'equipment_incurred_or_remaining_split':None,'binding_qualifying_financing_at_filing':False,
                    'exercise_building_2':'2026-09-29','building_2_negotiation_days_subject_to_mutual_extension':60,
                    'illustrative_bridge_carry':{'principal':1,'simple_annual_rate':.08,'months':6,'carry_B':timing(1,.08,6),'reported_Nscale_rate':False},
                    'portfolio_average_prepayment_is_not_a_Monarch_input':True},
          'settlement_check':['Final price, primary and secondary share counts; actual option exercise and dated settlement.',
                              'Issuer receipt from each concurrent investor, including any Google note substituted for shares; cash tax remittance on the RSU release.',
                              'Binding site and equipment facilities for Monarch, permitted draw purposes, tranches and acceptance dates.']}
    (HERE/'pricing-desk.json').write_text(json.dumps(data,default=float,indent=2)+'\n',encoding='utf-8')
