import sys

import httpx

from config import config

def main() -> None:
	(username,) = sys.argv[1:]
	(data,) = httpx.get(f'https://api.punoted.net/v1/contracts/?username={username}&limit=200',
			headers={'X-Data-Token': config.punoted_api_key}).raise_for_status().json()
	data['Contracts'].sort(key=lambda c: c['DateEpochMs'])
	for contract in data['Contracts']:
		print(contract['ContractLocalId'], contract['PartnerName'], contract['Name'], contract['Preamble'])
		print(contract['Timestamp'])
		for condition in contract['Conditions']:
			match condition['Type']:
				case 'DELIVERY':
					if condition['Party'] == contract['Party']:
						direction = '→'
					else:
						direction = '←'
				case 'COMEX_PURCHASE_PICKUP':
					if condition['Party'] == contract['Party']:
						direction = '←'
					else:
						direction = '→'
					print('\t', direction, condition['MaterialAmount'], condition['MaterialTicker'])
				case 'PAYMENT':
					print('\t', condition['Amount'], condition['Currency'])
				case 'PROVISION_SHIPMENT' | 'PICKUP_SHIPMENT' | 'DELIVERY_SHIPMENT' | 'REPUTATION' | 'EXPLORATION' | 'REPAIR_SHIP' \
						| 'PROVISION' | 'LOAN_PAYOUT' | 'LOAN_INSTALLMENT':
					pass
				case _:
					raise AssertionError('unhandled condition type ' + condition['Type'])

if __name__ == '__main__':
	main()
