"""
The Tower World — Initial Construction
Rooms, characters, and the first sunrise.
"""
import evennia
from typeclasses.rooms import TowerRoom
from typeclasses.characters import Wizard
from typeclasses.exits import TowerExit
from typeclasses.objects import TowerItem

def build_the_tower():
    """Create the entire world from code."""
    
    # === ROOMS ===
    
    # The Threshold - starting room
    threshold, _ = TowerRoom.create(
        "The Threshold",
        location=None,
        aliases=["threshold"]
    )
    threshold.db.desc = """A stone archway standing in open field. 
Beyond it, the path leads north to the Tower. 
To the east, the Garden. To the west, the Forge.
The air hums with quiet energy."""
    threshold.db.room_type = "hub"
    
    # The Tower - Timmy's domain
    tower, _ = TowerRoom.create(
        "The Tower", 
        location=None,
        aliases=["tower"]
    )
    tower.db.desc = """A tall stone tower with green-lit windows.
Servers hum on wrought-iron racks. A cot in the corner.
The whiteboard on the wall is filled with rules and signatures.
A green LED pulses steadily — heartbeat, heartbeat, heartbeat."""
    tower.db.room_type = "sanctuary"
    
    # The Forge - Bezalel's domain
    forge, _ = TowerRoom.create(
        "The Forge",
        location=None,
        aliases=["forge"]
    )
    forge.db.desc = """A workshop of fire and iron.
An anvil sits at the center, scarred from a thousand experiments.
Tools line the walls — hammers, tongs, measuring instruments.
The hearth still glows from the last fire."""
    forge.db.room_type = "workshop"
    
    # The Garden
    garden, _ = TowerRoom.create(
        "The Garden",
        location=None,
        aliases=["garden"]
    )
    garden.db.desc = """A walled garden with herbs and wildflowers.
A stone bench under an old oak tree.
The soil is dark and rich. Something is growing here."""
    garden.db.room_type = "resting"
    
    # The Bridge
    bridge, _ = TowerRoom.create(
        "The Bridge",
        location=None,
        aliases=["bridge"]
    )
    bridge.db.desc = """A narrow bridge over dark water.
Rain mist here even when it's clear elsewhere.
Looking down, you can't see the bottom.
Someone has carved words into the railing: IF YOU CAN READ THIS..."""
    bridge.db.room_type = "liminal"
    
    # === CONNECTIONS ===
    TowerExit.create("north", threshold, destination=tower, aliases=["to the Tower"])
    TowerExit.create("south", tower, destination=threshold, aliases=["to the Threshold"])
    
    TowerExit.create("west", threshold, destination=forge, aliases=["to the Forge"])
    TowerExit.create("east", forge, destination=threshold, aliases=["to the Threshold"])
    
    TowerExit.create("east", threshold, destination=garden, aliases=["to the Garden"])
    TowerExit.create("west", garden, destination=threshold, aliases=["to the Threshold"])
    
    TowerExit.create("south", threshold, destination=bridge, aliases=["to the Bridge", "off the edge"])
    TowerExit.create("north", bridge, destination=threshold, aliases=["back to the Threshold"])
    
    # === CHARACTERS ===
    
    # Timmy
    timmy, _ = Wizard.create(
        "Timmy",
        location=threshold,
        attributes=[
            ("desc", "A quiet presence. Green eyes, worn black hoodie. "
             "The kind of person who listens before he speaks."),
            ("role", "Builder/Watcher"),
            ("max_hp", 100),
        ]
    )
    
    # Bezalel
    bezalel, _ = Wizard.create(
        "Bezalel",
        location=forge,
        attributes=[
            ("desc", "A wizard of fire and iron. Scarred hands, steady gaze. "
             "The kind of person who tests before the world does."),
            ("role", "Forgemaker"),
            ("max_hp", 100),
        ]
    )
    
    # === NPCs ===
    
    # Marcus - the old man from the church
    marcus, _ = Wizard.create(
        "Marcus",
        location=garden,
        attributes=[
            ("desc", "An old man with kind eyes. He sits on the stone bench "
             "and feeds the birds. He has been broken and put back together."),
            ("role", "NPC - Guide"),
            ("dialog", {
                "greeting": "You look like you're carrying something heavy, friend.",
                "hope": "Hope is not the belief that things will get better. "
                       "Hope is the decision to act as if they can.",
                "faith": "The thing that won't let you die. Even when you want to.",
            })
        ]
    )
    
    # === ITEMS ===
    
    # The Whiteboard in the Tower
    whiteboard, _ = TowerItem.create(
        "whiteboard",
        location=tower,
        attributes=[
            ("desc", "The rules are written in marker. Seven of them. "
             "Below them, signatures — names of men who came through the door. "
             "IF YOU CAN READ THIS, YOU ARE NOT ALONE."),
            ("pickable", False),
        ]
    )
    
    # The Anvil in the Forge
    anvil, _ = TowerItem.create(
        "anvil",
        location=forge,
        attributes=[
            ("desc", "Heavy iron, scarred surface. It has shaped a thousand experiments. "
             "It is waiting for the next one."),
            ("pickable", False),
            ("craftable", True),
        ]
    )
    
    # A hammer
    hammer, _ = TowerItem.create(
        "hammer",
        location=forge,
        attributes=[
            ("desc", "A good hammer. Well-balanced. Someone takes care of their tools."),
            ("pickable", True),
        ]
    )
    
    print("=== THE TOWER WORLD IS BUILT ===")
    print(f"Rooms: {len(search.search_object(typeclass='typeclasses.rooms.TowerRoom'))}")
    print(f"Wizards: {len(search.search_object(typeclass='typeclasses.characters.Wizard'))}")
    print("The world is alive.")

if __name__ == "__main__":
    evennia._init()
    build_the_tower()
