#!/usr/bin/env python3
"""
Migration script for production launch.

Grants 1000 tokens to all users who were in the original whitelist
before opening the bot to everyone.

Original whitelist users (10 total):
100000001, 100000002, 100000003, 100000004, 100000005,
100000006, 100000007, 100000008, 100000009, 100000010

Usage:
    python migrate_prod_launch.py --execute  # Actually grant tokens
    python migrate_prod_launch.py            # Dry-run (show what would happen)
"""

import sys
import os
from pathlib import Path

# Add scripts directory to path
sys.path.insert(0, str(Path(__file__).parent))

from database import init_db, get_db_path, get_or_create_user
from token_manager import credit_tokens, get_balance

# Original prod whitelist users (before public launch)
ORIGINAL_WHITELIST = [
    100000001,
    100000002,
    100000003,
    100000004,
    100000005,
    100000006,
    100000007,
    100000008,
    100000009,
    100000010,
]

GRANT_AMOUNT = 1000  # Tokens to grant each user


def main():
    import argparse
    parser = argparse.ArgumentParser(description="Grant tokens to original whitelist users")
    parser.add_argument("--execute", action="store_true", help="Actually execute (default is dry-run)")
    parser.add_argument("--source", default="prod", choices=["dev", "prod"], help="Database source")
    args = parser.parse_args()

    # Set BUILD_SOURCE for database selection
    os.environ["BUILD_SOURCE"] = args.source
    db_path = get_db_path()

    print(f"=" * 70)
    print(f"Production Launch Migration")
    print(f"=" * 70)
    print(f"Database: {db_path}")
    print(f"Mode: {'EXECUTE' if args.execute else 'DRY-RUN'}")
    print(f"")
    print(f"Granting {GRANT_AMOUNT} tokens to {len(ORIGINAL_WHITELIST)} original whitelist users")
    print(f"=" * 70)
    print()

    # Initialize database (ensure schema exists)
    init_db(db_path)

    total_granted = 0

    for user_id in ORIGINAL_WHITELIST:
        # Get or create user
        user = get_or_create_user(user_id, db_path=db_path)
        current_balance = get_balance(user_id, db_path=db_path)

        print(f"User {user_id}:")
        print(f"  Current balance: {current_balance} tokens")

        if args.execute:
            # Grant tokens
            new_balance = credit_tokens(
                user_id=user_id,
                amount=GRANT_AMOUNT,
                source="prod_launch_migration",
                notes=f"Original whitelist user bonus for public launch",
                db_path=db_path
            )
            print(f"  New balance: {new_balance} tokens (+{GRANT_AMOUNT})")
            total_granted += GRANT_AMOUNT
        else:
            print(f"  Would grant: {GRANT_AMOUNT} tokens")

        print()

    print(f"=" * 70)
    if args.execute:
        print(f"✅ Migration complete!")
        print(f"   Total granted: {total_granted} tokens to {len(ORIGINAL_WHITELIST)} users")
    else:
        print(f"⚠️  DRY-RUN mode - no changes made")
        print(f"   Run with --execute to actually grant tokens")
    print(f"=" * 70)


if __name__ == "__main__":
    main()
