#!/usr/bin/env python3
"""
Update locations.json with missing interactables from world_loader_old.py
and spawn configurations from npcs_old.py
"""
import json
from pathlib import Path

# Load current locations.json
locations_file = Path("gamedata/locations.json")
with open(locations_file, 'r') as f:
    data = json.load(f)

# Helper to find location by ID
def find_location(loc_id):
    for loc in data['locations']:
        if loc['id'] == loc_id:
            return loc
    return None

# Add missing interactables to start_point
start_point = find_location('start_point')
if start_point and 'start_point_sedan' not in start_point.get('interactables', {}):
    if 'interactables' not in start_point:
        start_point['interactables'] = {}
    
    start_point['interactables']['start_point_sedan'] = {
        "id": "sedan",
        "name": "🚗 Rusty Sedan",
        "image_path": "images/interactables/sedan.png",
        "actions": {
            "search_glovebox": {
                "id": "search_glovebox",
                "label": "🔎 Search Glovebox",
                "stamina_cost": 1,
                "outcomes": {
                    "success": {"text": "You find a half-eaten [Stale Chocolate Bar].", "items_reward": {"stale_chocolate_bar": 1}, "damage_taken": 0},
                    "failure": {"text": "The glovebox is empty except for dust and old receipts.", "items_reward": {}, "damage_taken": 0}
                }
            },
            "pop_trunk": {
                "id": "pop_trunk",
                "label": "🔧 Pop the Trunk",
                "stamina_cost": 3,
                "outcomes": {
                    "success": {"text": "With a great heave, you pry the trunk open and find a [Tire Iron]!", "items_reward": {"tire_iron": 1}, "damage_taken": 0},
                    "failure": {"text": "The trunk is rusted shut. You can't get it open.", "items_reward": {}, "damage_taken": 0}
                }
            }
        }
    }
    
    start_point['interactables']['start_point_dumpster'] = {
        "id": "dumpster",
        "name": "🗑️ Dumpster",
        "image_path": "images/interactables/dumpster.png",
        "actions": {
            "search_dumpster": {
                "id": "search_dumpster",
                "label": "🔎 Dig Through Trash",
                "stamina_cost": 2,
                "outcomes": {
                    "success": {"text": "Despite the smell, you find some [Plastic Bottles] and [Cloth Scraps].", "items_reward": {"plastic_bottles": 3, "cloth_scraps": 2}, "damage_taken": 0},
                    "failure": {"text": "Just rotting garbage. Nothing useful.", "items_reward": {}, "damage_taken": 0},
                    "critical_failure": {"text": "You disturb a nest of rats! They bite you! (-8 HP)", "items_reward": {}, "damage_taken": 8}
                }
            }
        }
    }

# Add missing interactables to subway_tunnels
subway_tunnels = find_location('subway_tunnels')
if subway_tunnels:
    if 'interactables' not in subway_tunnels:
        subway_tunnels['interactables'] = {}
    
    subway_tunnels['interactables']['subway_train_sedan'] = {
        "id": "sedan",
        "name": "🚗 Rusty Sedan",
        "image_path": "images/interactables/sedan.png",
        "actions": {
            "search_glovebox": {
                "id": "search_glovebox",
                "label": "🔎 Search Glovebox",
                "stamina_cost": 1,
                "outcomes": {
                    "success": {"text": "You find a half-eaten [Stale Chocolate Bar].", "items_reward": {"stale_chocolate_bar": 1}, "damage_taken": 0},
                    "failure": {"text": "The glovebox is empty except for dust and old receipts.", "items_reward": {}, "damage_taken": 0}
                }
            },
            "pop_trunk": {
                "id": "pop_trunk",
                "label": "🔧 Pop the Trunk",
                "stamina_cost": 3,
                "outcomes": {
                    "success": {"text": "With a great heave, you pry the trunk open and find a [Tire Iron]!", "items_reward": {"tire_iron": 1}, "damage_taken": 0},
                    "failure": {"text": "The trunk is rusted shut. You can't get it open.", "items_reward": {}, "damage_taken": 0}
                }
            }
        }
    }
    
    subway_tunnels['interactables']['subway_medkit'] = {
        "id": "medkit",
        "name": "🏥 Medical Supply Cabinet",
        "image_path": "images/interactables/medkit.png",
        "actions": {
            "search_medkit": {
                "id": "search_medkit",
                "label": "🔎 Search Cabinet",
                "stamina_cost": 2,
                "outcomes": {
                    "success": {"text": "Jackpot! You find a [First Aid Kit] and some [Bandages]!", "items_reward": {"first_aid_kit": 1, "bandage": 2}, "damage_taken": 0},
                    "failure": {"text": "The cabinet is empty. Someone got here first.", "items_reward": {}, "damage_taken": 0}
                }
            }
        }
    }
    
    subway_tunnels['interactables']['subway_rubble'] = {
        "id": "rubble",
        "name": "Pile of Rubble",
        "image_path": "images/interactables/rubble.png",
        "actions": {
            "search": {
                "id": "search",
                "label": "🔎 Search Rubble",
                "stamina_cost": 2,
                "outcomes": {
                    "success": {"text": "You dig through the debris and find some [Scrap Metal].", "items_reward": {"scrap_metal": 2}, "damage_taken": 0},
                    "failure": {"text": "The pile seems to have been picked clean already.", "items_reward": {}, "damage_taken": 0},
                    "critical_failure": {"text": "You cut your hand on a sharp piece of glass! (-5 HP)", "items_reward": {}, "damage_taken": 5}
                }
            }
        }
    }

# Add missing interactables to office_interior
office_interior = find_location('office_interior')
if office_interior:
    if 'interactables' not in office_interior:
        office_interior['interactables'] = {}
    
    office_interior['interactables']['office_desk1'] = {
        "id": "rubble",
        "name": "Pile of Rubble",
        "image_path": "images/interactables/rubble.png",
        "actions": {
            "search": {
                "id": "search",
                "label": "🔎 Search Rubble",
                "stamina_cost": 2,
                "outcomes": {
                    "success": {"text": "You dig through the debris and find some [Scrap Metal].", "items_reward": {"scrap_metal": 2}, "damage_taken": 0},
                    "failure": {"text": "The pile seems to have been picked clean already.", "items_reward": {}, "damage_taken": 0},
                    "critical_failure": {"text": "You cut your hand on a sharp piece of glass! (-5 HP)", "items_reward": {}, "damage_taken": 5}
                }
            }
        }
    }
    
    office_interior['interactables']['office_desk2'] = {
        "id": "rubble",
        "name": "Pile of Rubble",
        "image_path": "images/interactables/rubble.png",
        "actions": {
            "search": {
                "id": "search",
                "label": "🔎 Search Rubble",
                "stamina_cost": 2,
                "outcomes": {
                    "success": {"text": "You dig through the debris and find some [Scrap Metal].", "items_reward": {"scrap_metal": 2}, "damage_taken": 0},
                    "failure": {"text": "The pile seems to have been picked clean already.", "items_reward": {}, "damage_taken": 0},
                    "critical_failure": {"text": "You cut your hand on a sharp piece of glass! (-5 HP)", "items_reward": {}, "damage_taken": 5}
                }
            }
        }
    }
    
    office_interior['interactables']['office_corner'] = {
        "id": "house",
        "name": "🏚️ Abandoned House",
        "image_path": "images/interactables/house.png",
        "actions": {
            "search_house": {
                "id": "search_house",
                "label": "🔎 Search House",
                "stamina_cost": 3,
                "outcomes": {
                    "success": {"text": "You find some useful supplies: [Canned Beans], [Bottled Water], and [Cloth Scraps]!", "items_reward": {"canned_beans": 1, "bottled_water": 1, "cloth_scraps": 3}, "damage_taken": 0},
                    "failure": {"text": "The house has already been thoroughly looted. Nothing remains.", "items_reward": {}, "damage_taken": 0},
                    "critical_failure": {"text": "The floor collapses beneath you! (-10 HP)", "items_reward": {}, "damage_taken": 10}
                }
            }
        }
    }

# Update danger_config from npcs_old.py
data['danger_config'] = {
    "start_point": {"danger_level": 0, "encounter_rate": 0.0, "wandering_chance": 0.0},
    "gas_station": {"danger_level": 0, "encounter_rate": 0.0, "wandering_chance": 0.0},
    "residential": {"danger_level": 1, "encounter_rate": 0.10, "wandering_chance": 0.20},
    "park": {"danger_level": 1, "encounter_rate": 0.10, "wandering_chance": 0.20},
    "clinic": {"danger_level": 2, "encounter_rate": 0.20, "wandering_chance": 0.35},
    "plaza": {"danger_level": 2, "encounter_rate": 0.15, "wandering_chance": 0.30},
    "warehouse": {"danger_level": 2, "encounter_rate": 0.18, "wandering_chance": 0.32},
    "warehouse_interior": {"danger_level": 2, "encounter_rate": 0.22, "wandering_chance": 0.40},
    "overpass": {"danger_level": 3, "encounter_rate": 0.30, "wandering_chance": 0.45},
    "office_building": {"danger_level": 3, "encounter_rate": 0.25, "wandering_chance": 0.40},
    "office_interior": {"danger_level": 3, "encounter_rate": 0.35, "wandering_chance": 0.50},
    "subway": {"danger_level": 4, "encounter_rate": 0.35, "wandering_chance": 0.50},
    "subway_tunnels": {"danger_level": 4, "encounter_rate": 0.45, "wandering_chance": 0.65}
}

# Update spawn_config from npcs_old.py
data['spawn_config'] = {
    "start_point": [],
    "gas_station": [],
    "residential": [
        {"npc_id": "feral_dog", "weight": 60},
        {"npc_id": "mutant_rat", "weight": 40}
    ],
    "park": [
        {"npc_id": "feral_dog", "weight": 50},
        {"npc_id": "mutant_rat", "weight": 50}
    ],
    "clinic": [
        {"npc_id": "infected_human", "weight": 40},
        {"npc_id": "mutant_rat", "weight": 30},
        {"npc_id": "scavenger", "weight": 30}
    ],
    "plaza": [
        {"npc_id": "raider_scout", "weight": 40},
        {"npc_id": "scavenger", "weight": 35},
        {"npc_id": "feral_dog", "weight": 25}
    ],
    "warehouse": [
        {"npc_id": "raider_scout", "weight": 45},
        {"npc_id": "scavenger", "weight": 35},
        {"npc_id": "mutant_rat", "weight": 20}
    ],
    "warehouse_interior": [
        {"npc_id": "raider_scout", "weight": 50},
        {"npc_id": "scavenger", "weight": 30},
        {"npc_id": "mutant_rat", "weight": 20}
    ],
    "overpass": [
        {"npc_id": "raider_scout", "weight": 50},
        {"npc_id": "infected_human", "weight": 30},
        {"npc_id": "scavenger", "weight": 20}
    ],
    "office_building": [
        {"npc_id": "raider_scout", "weight": 45},
        {"npc_id": "infected_human", "weight": 35},
        {"npc_id": "scavenger", "weight": 20}
    ],
    "office_interior": [
        {"npc_id": "infected_human", "weight": 50},
        {"npc_id": "raider_scout", "weight": 30},
        {"npc_id": "scavenger", "weight": 20}
    ],
    "subway": [
        {"npc_id": "infected_human", "weight": 50},
        {"npc_id": "raider_scout", "weight": 30},
        {"npc_id": "mutant_rat", "weight": 20}
    ],
    "subway_tunnels": [
        {"npc_id": "infected_human", "weight": 60},
        {"npc_id": "raider_scout", "weight": 25},
        {"npc_id": "mutant_rat", "weight": 15}
    ]
}

# Save updated file
with open(locations_file, 'w') as f:
    json.dump(data, f, indent=2)

print("✅ Updated locations.json with:")
print("  - Missing interactables for start_point, subway_tunnels, office_interior")
print("  - Complete danger_config from npcs_old.py")
print("  - Complete spawn_config from npcs_old.py")
