"""Common utilities in testing the modified MutPy fork."""

import random
import string
import os


def generate_random_string(length=100):
    """Generate for the purpose of observing killed mutant"""
    characters = string.ascii_letters + string.digits  # Includes uppercase, lowercase, and digits
    return ''.join(random.choices(characters, k=length))

counter_file = 'test_call_count.csv'
overall_faults = 8

def initialize_counter():
    """Initialize the counter file with 0 if it doesn't exist."""
    if not os.path.exists(counter_file):
        with open(counter_file, "w") as file:
            file.write("0")

def increment_counter():
    """Increment the counter value and write it back to the file."""
    with open(counter_file, "r") as file:
        count = int(file.read().strip())
    count += 1
    with open(counter_file, "w") as file:
        file.write(str(count))

def get_counter():
    """Read and return the current counter value."""
    with open(counter_file, "r") as file:
        return int(file.read().strip())

def reset_counter():
    """Reset the counter value to 0."""
    with open(counter_file, "w") as file:
        file.write("0")

def delete_counter():
    """Delete the counter file if it exists."""
    if os.path.exists(counter_file):
        os.remove(counter_file)
        print(f"Counter file '{counter_file}' has been deleted.")
    else:
        print(f"Counter file '{counter_file}' does not exist.")


def last_test_routine(is_rapfd=False):
    increment_counter()
    future_count = get_counter()

    if future_count == overall_faults + 1:
        delete_counter()


def simulate_test_case_run(call_count, fault_ids_to_detect):
    """Simulate run for mutant number <call_count>.
    Detect failure if this mutant is identified from <fault_ids_to_detect>.
    """
    if call_count in fault_ids_to_detect:
        raise Exception(generate_random_string())
    assert True