"""Run only with assigned credentials. Example is a notification, not a payment."""
import json
import os
import time
import uuid
import base64
import httpx
from cryptography.hazmat.primitives.ciphers.aead import AESGCM

def encrypt(payload, key_b64):
    key = base64.b64decode(key_b64, validate=True)
    if len(key) != 32:
        raise ValueError('Expected 32-byte encryption key')
    nonce = os.urandom(12)
    aad = ('hipayx-v1|POST|/v1/inward-remittance-notifications|' + payload['client_id']).encode()
    ciphertext = AESGCM(key).encrypt(nonce, json.dumps(payload, ensure_ascii=False).encode(), aad)
    return {'nonce': base64.b64encode(nonce).decode(),
            'ciphertext': base64.b64encode(ciphertext).decode()}

if __name__ == '__main__':
    client = os.environ['HIPAYX_CLIENT_ID']
    payload = {'client_id': client, 'notification_id': str(uuid.uuid4()),
               'batch_id': 'EXAMPL' + time.strftime('%y%m%d', time.gmtime()) + '001',
               'timestamp': int(time.time()), 'recipient_name': 'EXAMPLE COMPANY ONLY',
               'recipient_account': 'EXAMPLE000001', 'recipient_swift': 'TESTHKHHXXX',
               'amount': '100.00', 'currency': 'USD', 'remarks': 'Integration test - no funds moved'}
    encrypted = encrypt(payload, os.environ['HIPAYX_ENCRYPTION_KEY'])
    response = httpx.post(os.environ['HIPAYX_API_URL'], json=encrypted,
                          headers={'Authorization': 'Bearer ' + os.environ['HIPAYX_API_KEY'],
                                   'X-Client-Id': client}, timeout=30)
    print(response.status_code, response.text)
