import time
import json
import sys,os

class TransactionLogger:
    def __init__(self):
        self.start_time = int(time.time() * 1000)
        self.transaction_log = {
            'statusCode': '200',
            'status': 'Success',
            'backendCall': []
        }
        self.is_initialized = True

    def get_response_time(self):
        return int(time.time() * 1000) - self.start_time

    def set_backend_details(self, backend_call):
        self.transaction_log['backendCall'].append(backend_call)

    def add_bam_attributes(self, key, value):
        self.transaction_log[key] = value

    def set_transaction_status(self, status, status_code, error_description=None):
        self.transaction_log['status'] = status
        self.transaction_log['statusCode'] = status_code
        if error_description:
            self.transaction_log['errorDescription'] = error_description

    def get_service_latency_time(self):
        total_latency = sum(call['latency'] for call in self.transaction_log['backendCall'])
        return total_latency

    def log_transaction(self):
        self.transaction_log['svcLatency'] = self.get_response_time()
        if self.transaction_log['statusCode'] in ['200', '201', '202']:
            print(f'transactionLog: {json.dumps(self.transaction_log)}')
        else:
            print(f'transactionLog: {json.dumps(self.transaction_log)}', file=sys.stderr)
        self.transaction_log = {}

    def log_object(self, obj):
        if obj:
            for key, value in obj.items():
                self.add_bam_attributes(key, value)

    def log_event_attributes(self, event):
        if event:
            authorizer = event.get('requestContext', {}).get('authorizer', {})
            self.add_bam_attributes('client_id', authorizer.get('principalId'))
            self.add_bam_attributes('client_name', authorizer.get('client_name'))
            self.add_bam_attributes('httpMethod', event.get('httpMethod'))
            self.add_bam_attributes('URI', event.get('path'))
            trace_id = os.environ.get('_X_AMZN_TRACE_ID').replace('Root=', '')
            self.add_bam_attributes('traceId', trace_id)