import unreal


CHARACTER_BLUEPRINT_PATH = "/Game/Agrarian/Blueprints/Characters/BP_AgrarianPlayerCharacter"
CONTROLLER_BLUEPRINT_PATH = "/Game/Agrarian/Blueprints/Characters/BP_AgrarianPlayerController"
GAME_MODE_BLUEPRINT_PATH = "/Game/Agrarian/Blueprints/Characters/BP_AgrarianGameMode"
EXPECTED_RECIPE_IDS = [
    "campfire",
    "primitive_frame",
    "primitive_wall_panel",
    "primitive_roof_panel",
    "primitive_shelter",
    "basic_tool",
    "simple_container",
    "bandage",
]


def class_path(value):
    if not value:
        return ""
    return value.get_path_name().split(".")[0]


def load_blueprint_class(path):
    generated_class = unreal.EditorAssetLibrary.load_blueprint_class(path)
    if not generated_class:
        raise RuntimeError(f"Could not load generated Blueprint class: {path}")
    return generated_class


def main():
    failures = []

    for path in [CHARACTER_BLUEPRINT_PATH, CONTROLLER_BLUEPRINT_PATH, GAME_MODE_BLUEPRINT_PATH]:
        if not unreal.EditorAssetLibrary.does_asset_exist(path):
            failures.append(f"{path} missing")

    character_class = load_blueprint_class(CHARACTER_BLUEPRINT_PATH)
    controller_class = load_blueprint_class(CONTROLLER_BLUEPRINT_PATH)
    game_mode_class = load_blueprint_class(GAME_MODE_BLUEPRINT_PATH)
    game_mode_cdo = unreal.get_default_object(game_mode_class)

    default_pawn_path = class_path(game_mode_cdo.get_editor_property("default_pawn_class"))
    player_controller_path = class_path(game_mode_cdo.get_editor_property("player_controller_class"))

    if default_pawn_path != CHARACTER_BLUEPRINT_PATH:
        failures.append(
            f"Game mode default pawn expected {CHARACTER_BLUEPRINT_PATH}, got {default_pawn_path}"
        )

    if player_controller_path != CONTROLLER_BLUEPRINT_PATH:
        failures.append(
            "Game mode player controller expected "
            f"{CONTROLLER_BLUEPRINT_PATH}, got {player_controller_path}"
        )

    character_cdo = unreal.get_default_object(character_class)
    if not character_cdo:
        failures.append(f"{CHARACTER_BLUEPRINT_PATH} CDO missing")
    else:
        crafting_component = character_cdo.get_editor_property("crafting_component")
        recipe_assets = crafting_component.get_editor_property("known_recipe_assets") if crafting_component else []
        recipe_ids = [
            str(asset.get_editor_property("recipe").get_editor_property("recipe_id"))
            for asset in recipe_assets
            if asset
        ]
        if recipe_ids != EXPECTED_RECIPE_IDS:
            failures.append(
                "BP_AgrarianPlayerCharacter known recipes expected "
                f"{EXPECTED_RECIPE_IDS}, got {recipe_ids}"
            )

    controller_cdo = unreal.get_default_object(controller_class)
    if not controller_cdo:
        failures.append(f"{CONTROLLER_BLUEPRINT_PATH} CDO missing")

    if failures:
        raise RuntimeError("Agrarian player Blueprint verification failed: " + "; ".join(failures))

    unreal.log(
        "Agrarian player Blueprint verification complete: "
        "native Agrarian character, controller, and game mode asset paths load correctly."
    )


main()
