import unreal


ITEM_FOLDER = "/Game/Agrarian/DataAssets/Items"

ITEMS = [
    {
        "asset": "DA_Item_Wood",
        "item_id": "wood",
        "display_name": "Wood",
        "description": "Usable branches, logs, and rough-cut timber for fires, tools, and early structures.",
        "item_type": unreal.AgrarianItemType.RESOURCE,
        "unit_weight": 1.0,
        "max_stack_size": 99,
    },
    {
        "asset": "DA_Item_Stone",
        "item_id": "stone",
        "display_name": "Stone",
        "description": "Field stone suitable for crude tools, fire rings, and primitive construction.",
        "item_type": unreal.AgrarianItemType.RESOURCE,
        "unit_weight": 1.5,
        "max_stack_size": 99,
    },
    {
        "asset": "DA_Item_Fiber",
        "item_id": "fiber",
        "display_name": "Fiber",
        "description": "Plant fiber that can be twisted, woven, or bound into survival gear and simple structures.",
        "item_type": unreal.AgrarianItemType.RESOURCE,
        "unit_weight": 0.1,
        "max_stack_size": 99,
    },
    {
        "asset": "DA_Item_Food",
        "item_id": "food",
        "display_name": "Food",
        "description": "Foraged edible food that can stave off hunger in the earliest survival loop.",
        "item_type": unreal.AgrarianItemType.FOOD,
        "unit_weight": 0.3,
        "max_stack_size": 50,
    },
    {
        "asset": "DA_Item_Meat",
        "item_id": "meat",
        "display_name": "Meat",
        "description": "Raw harvested meat that should be cooked or preserved before long-term use.",
        "item_type": unreal.AgrarianItemType.FOOD,
        "unit_weight": 0.5,
        "max_stack_size": 50,
    },
    {
        "asset": "DA_Item_Hide",
        "item_id": "hide",
        "display_name": "Hide",
        "description": "Animal hide used for shelter, clothing, containers, and other early craft work.",
        "item_type": unreal.AgrarianItemType.RESOURCE,
        "unit_weight": 0.7,
        "max_stack_size": 50,
    },
    {
        "asset": "DA_Item_PrimitiveFrame",
        "item_id": "primitive_frame",
        "display_name": "Primitive Frame",
        "description": "A lashed support frame used as the backbone for early shelters and simple buildables.",
        "item_type": unreal.AgrarianItemType.STRUCTURE,
        "unit_weight": 3.0,
        "max_stack_size": 20,
    },
    {
        "asset": "DA_Item_PrimitiveWallPanel",
        "item_id": "primitive_wall_panel",
        "display_name": "Primitive Wall Panel",
        "description": "A crude wall panel built from wood and fiber for early shelter construction.",
        "item_type": unreal.AgrarianItemType.STRUCTURE,
        "unit_weight": 4.0,
        "max_stack_size": 20,
    },
    {
        "asset": "DA_Item_PrimitiveRoofPanel",
        "item_id": "primitive_roof_panel",
        "display_name": "Primitive Roof Panel",
        "description": "A simple roof panel made from lashed branches, fiber, and cover material.",
        "item_type": unreal.AgrarianItemType.STRUCTURE,
        "unit_weight": 4.0,
        "max_stack_size": 20,
    },
    {
        "asset": "DA_Item_Campfire",
        "item_id": "campfire",
        "display_name": "Campfire",
        "description": "A placeable fire ring for warmth, light, and early cooking.",
        "item_type": unreal.AgrarianItemType.STRUCTURE,
        "unit_weight": 12.0,
        "max_stack_size": 5,
    },
    {
        "asset": "DA_Item_PrimitiveShelter",
        "item_id": "primitive_shelter",
        "display_name": "Primitive Shelter",
        "description": "A compact early shelter that provides basic weather protection.",
        "item_type": unreal.AgrarianItemType.STRUCTURE,
        "unit_weight": 25.0,
        "max_stack_size": 1,
    },
    {
        "asset": "DA_Item_SimpleContainer",
        "item_id": "simple_container",
        "display_name": "Simple Container",
        "description": "A crude hide-and-frame container for early storage, trade, and later placed-container systems.",
        "item_type": unreal.AgrarianItemType.STRUCTURE,
        "unit_weight": 8.0,
        "max_stack_size": 5,
    },
    {
        "asset": "DA_Item_BasicTool",
        "item_id": "basic_tool",
        "display_name": "Basic Tool",
        "description": "A crude stone-and-wood tool for the first gather and build loops.",
        "item_type": unreal.AgrarianItemType.TOOL,
        "unit_weight": 1.2,
        "max_stack_size": 10,
    },
    {
        "asset": "DA_Item_Bandage",
        "item_id": "bandage",
        "display_name": "Bandage",
        "description": "A simple treatment item made from hide and fiber.",
        "item_type": unreal.AgrarianItemType.MEDICINE,
        "unit_weight": 0.1,
        "max_stack_size": 20,
    },
]


def create_or_load_item_asset(asset_name):
    path = f"{ITEM_FOLDER}/{asset_name}"
    existing = unreal.EditorAssetLibrary.load_asset(path)
    if existing:
        return existing

    factory = unreal.DataAssetFactory()
    factory.set_editor_property("data_asset_class", unreal.AgrarianItemDefinitionAsset)
    asset_tools = unreal.AssetToolsHelpers.get_asset_tools()
    asset = asset_tools.create_asset(asset_name, ITEM_FOLDER, unreal.AgrarianItemDefinitionAsset, factory)
    if not asset:
        raise RuntimeError(f"Could not create {path}")
    return asset


def apply_definition(asset, item):
    definition = unreal.AgrarianItemDefinition()
    definition.set_editor_property("item_id", item["item_id"])
    definition.set_editor_property("display_name", item["display_name"])
    definition.set_editor_property("description", item["description"])
    definition.set_editor_property("item_type", item["item_type"])
    definition.set_editor_property("unit_weight", item["unit_weight"])
    definition.set_editor_property("max_stack_size", item["max_stack_size"])
    asset.set_editor_property("definition", definition)
    unreal.EditorAssetLibrary.save_loaded_asset(asset)


def main():
    unreal.EditorAssetLibrary.make_directory(ITEM_FOLDER)
    for item in ITEMS:
        asset = create_or_load_item_asset(item["asset"])
        apply_definition(asset, item)
        unreal.log(f"Configured item definition: {item['item_id']} -> {ITEM_FOLDER}/{item['asset']}")

    unreal.log("Agrarian item definition setup complete.")


main()
