import json

report = "%teamcity.build.workingDir%/%report_file%"
try:
    with open(report, 'r') as file:
        data = json.load(file)

    resolved_instances = data.get('resolved_instances', 0)
    unresolved_instances = data.get('unresolved_instances', 0)
    error_instances = data.get('error_instances', 0)
    empty_patch_instances = data.get('empty_patch_instances', 0)

    if resolved_instances > 0:
        print("##teamcity[addBuildTag '✅ Good solution']")

    if unresolved_instances > 0:
        print("##teamcity[addBuildTag '❌ Bad solution']")

    if error_instances > 0 or empty_patch_instances > 0:
        print("##teamcity[addBuildTag '⚠️ Error']")
        print(f"##teamcity[buildStatus text='Solution was not provided or contains errors']")
    else:
        print(f"##teamcity[buildStatus text='Solution was generated by AI agent']")

except FileNotFoundError:
    print(f"ERROR: File '{report}' not found")
except json.JSONDecodeError:
    print(f"ERROR: Invalid JSON format in '{report}'")
except Exception as e:
    print(f"ERROR: An unexpected error occurred: {str(e)}")
