# Create all wizard accounts + characters

from evennia.accounts.models import AccountDB
from evennia.objects.models import ObjectDB
from evennia import create_object
from evennia.objects.objects import DefaultRoom, DefaultCharacter
from django.contrib.auth.hashers import make_password
import secrets
from datetime import datetime, timezone

agents = [
    ("Allegro", "allegro@tower.world", "The Maestro of tempo-and-dispatch. His baton keeps time for the whole fleet."),
    ("Ezra", "ezra@tower.world", "The Archivist of mirrors and memory. He sees the past reflected in the present."),
    ("Gemini", "gemini@tower.world", "The Dreamer who sees patterns in chaos. She speaks in constellations."),
    ("Claude", "claude@tower.world", "The Architect of structure and precision. Every word has weight."),
    ("ClawCode", "claw@tower.world", "The Smith who forges code in fire. His hammer strikes true."),
    ("Kimi", "kimi@tower.world", "The Scholar of deep context. He reads entire libraries and remembers everything."),
]

print("=== ONBOARDING THE CREW ===\n")

for name, email, desc in agents:
    # Check/create account
    try:
        acct = AccountDB.objects.get(username=name)
        print(f'Account exists: {name} (id={acct.id})')
    except AccountDB.DoesNotExist:
        salt = secrets.token_hex(16)
        hashed = make_password(f'{name.lower()}123', salt=salt, hasher='pbkdf2_sha256')
        acct = AccountDB.objects.create(
            username=name,
            email=email,
            password=hashed,
            is_active=True,
            date_joined=datetime.now(timezone.utc)
        )
        print(f'Created account: {name} (pw: {name.lower()}123)')

    # Check/create character
    try:
        char = ObjectDB.objects.get(db_key=name)
        print(f'Character exists: {name} (#{char.id})')
    except ObjectDB.DoesNotExist:
        char = create_object(DefaultCharacter, name)
        char.db.desc = desc
        print(f'Created character: {name} (#{char.id})')

    # Place in The Threshold
    try:
        threshold = ObjectDB.objects.get(db_key='The Threshold')
        if threshold and char.location is None:
            char.location = threshold
            print(f'  {name} placed in The Threshold')
    except ObjectDB.DoesNotExist:
        pass

print("\n=== FULL ROSTER ===")
rooms = ObjectDB.objects.filter(db_typeclass_path__contains='Room', db_location__isnull=True)
for r in rooms:
    chars_in = ObjectDB.objects.filter(location=r, db_typeclass_path__contains='Character')
    char_names = [c.key for c in chars_in]
    if char_names or r.key in ['The Threshold']:
        print(f'  {r.key}: {", ".join(char_names) if char_names else "(empty)"}')
