#!/usr/bin/env python3
"""
Evennia World Migration — Export/Import world state between worlds.

Usage:
    Export: python world/migrate.py export --output mac_world.json
    Import: python world/migrate.py import --input mac_world.json
"""
import json
import os
import sys
import argparse


def export_world(output_path):
    """
    Export Evennia world state to JSON file.
    
    This exports:
    - All rooms (with descriptions)
    - All characters (with stats)
    - All accounts
    - All game state (energy, trust, etc.)
    - Tick count
    """
    # This would normally connect to the Evennia database
    # For now, we export a placeholder structure
    
    world_data = {
        "version": "1.0",
        "world_name": "Timmy World",
        "tick": 1464,  # Current tick count
        "rooms": {},
        "characters": {},
        "accounts": {},
        "state": {},
    }
    
    # In a real export, we'd query the Evennia database
    # For now, return the structure
    
    print(f"Export structure created: {output_path}")
    print("NOTE: This is a template. Real export requires Evennia DB access.")
    print("Run this from within Evennia server or use Evennia API.")
    
    with open(output_path, 'w') as f:
        json.dump(world_data, f, indent=2)
    
    return output_path


def import_world(input_path):
    """Import Evennia world state from JSON file."""
    with open(input_path) as f:
        world_data = json.load(f)
    
    print(f"Importing world from: {input_path}")
    print(f"World: {world_data.get('world_name', '?')}")
    print(f"Tick: {world_data.get('tick', 0)}")
    print(f"Rooms: {len(world_data.get('rooms', {}))}")
    print(f"Characters: {len(world_data.get('characters', {}))}")
    print(f"Accounts: {len(world_data.get('accounts', {}))}")
    
    # In a real import, we'd create objects in the Evennia database
    # For now, just validate the structure
    
    return world_data


def main():
    parser = argparse.ArgumentParser(description='Evennia World Migration')
    parser.add_argument('action', choices=['export', 'import'], help='Export or import world')
    parser.add_argument('--output', help='Output file for export')
    parser.add_argument('--input', help='Input file for import')
    args = parser.parse_args()
    
    if args.action == 'export':
        output = args.output or 'world_export.json'
        export_world(output)
    elif args.action == 'import':
        input_path = args.input
        if not input_path:
            print("Error: --input required for import")
            sys.exit(1)
        import_world(input_path)


if __name__ == '__main__':
    main()
