"""
Basic skeleton of a mitmproxy addon.

Run as follows: mitmproxy -s anatomy.py
"""

import json
import os

# File to store events
FILE_PATH = "events_data.json"


def find_duplicates(events):
    seen = set()
    duplicates = []

    for event in events:
        # Create a unique identifier for each event based on 'action:url' and 'data.ts'
        if event.get('action') == 'heartbeat':
            identifier = (event.get('inc'), event.get('data', {}).get('ts', ''))

            if identifier in seen:
                duplicates.append(event)
            else:
                seen.add(identifier)

    return duplicates


class Counter:
    def __init__(self):
        self.num = 0

    def request(self, flow):
        # Decode the incoming data if it's in bytes format
        data = flow.request.content
        if isinstance(data, bytes):
            data = data.decode('utf-8')

        # Load new events from the incoming data
        new_events = json.loads(data).get("events", [])

        # Initialize a list for all events
        all_events = []

        # If the file exists, read the existing events
        if os.path.exists(FILE_PATH):
            with open(FILE_PATH, "r") as file:
                all_events = json.load(file)
        else:
            # Create an empty file if it does not exist
            with open(FILE_PATH, "w") as file:
                json.dump([], file)

        # Append new events to the existing events
        all_events.extend(new_events)

        # Check for duplicates
        duplicates = find_duplicates(all_events)

        # Log or handle duplicates as needed
        if duplicates:
            print("Found duplicates:")
            for duplicate in duplicates:
                print(duplicate)
        else:
            print("No duplicates found.")

        # Save the combined events back to the file
        with open(FILE_PATH, "w") as file:
            json.dump(all_events, file, indent=4)


addons = [Counter()]