"""Home command — teleport to personal zone."""

from mudlib.commands import CommandDefinition, register
from mudlib.commands.movement import send_nearby_message
from mudlib.housing import get_or_create_home
from mudlib.player import Player
from mudlib.store import save_player_home_zone
from mudlib.zone import Zone
from mudlib.zones import get_zone


async def cmd_home(player: Player, args: str) -> None:
    """Teleport to your personal zone, or return from it.

    Usage:
        home          — go to your home zone
        home return   — return to where you were before going home
    """
    from mudlib.commands.look import cmd_look

    arg = args.strip().lower()
    zone = player.location

    if arg == "return":
        # Return to previous location
        if player.return_location is None:
            await player.send("You have nowhere to return to.\r\n")
            return

        target_zone_name, target_x, target_y = player.return_location
        target_zone = get_zone(target_zone_name)
        if target_zone is None:
            await player.send("Your return destination no longer exists.\r\n")
            player.return_location = None
            return

        # Departure message
        if isinstance(zone, Zone):
            await send_nearby_message(
                player,
                player.x,
                player.y,
                f"{player.name} vanishes in a flash.\r\n",
            )

        # Move
        player.move_to(target_zone, x=target_x, y=target_y)
        player.return_location = None

        # Arrival message
        await send_nearby_message(
            player,
            player.x,
            player.y,
            f"{player.name} appears in a flash.\r\n",
        )

        await player.send("You return to where you were.\r\n")
        await cmd_look(player, "")
        return

    if arg:
        await player.send("Usage: home | home return\r\n")
        return

    # Go home
    home = get_or_create_home(player.name)

    # Save current location for return trip (only if not already at home)
    home_zone_name = f"home:{player.name.lower()}"
    if isinstance(zone, Zone) and zone.name != home_zone_name:
        player.return_location = (zone.name, player.x, player.y)

    # Departure message
    if isinstance(zone, Zone):
        await send_nearby_message(
            player,
            player.x,
            player.y,
            f"{player.name} vanishes in a flash.\r\n",
        )

    # Move to home spawn point
    player.move_to(home, x=home.spawn_x, y=home.spawn_y)

    # Update home_zone on player
    player.home_zone = home.name
    save_player_home_zone(player.name, home.name)

    # Arrival message (usually nobody else is in your home, but just in case)
    await send_nearby_message(
        player,
        player.x,
        player.y,
        f"{player.name} appears in a flash.\r\n",
    )

    await player.send("You arrive at your home.\r\n")
    await cmd_look(player, "")


register(
    CommandDefinition(
        "home",
        cmd_home,
        help="Teleport to your personal zone. 'home return' to go back.",
    )
)
