#!/usr/bin/env python3
"""Fix admin user password in database."""

import sys
import os

# Add project root to path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from backend.db.session import get_db
from backend.db.models import User
from backend.utils.password import hash_password, verify_password

def fix_admin_password():
    """Set admin user password to 'admin'."""
    db = next(get_db())
    try:
        admin_user = db.query(User).filter(User.username == 'admin').first()
        
        if not admin_user:
            print("❌ Admin user not found in database")
            return False
        
        # Set password to 'admin'
        new_hash = hash_password('admin')
        admin_user.password_hash = new_hash
        admin_user.is_active = True
        admin_user.is_admin = True
        db.commit()
        
        # Verify it works
        if verify_password('admin', new_hash):
            print("✅ Admin password updated successfully")
            print("   Username: admin")
            print("   Password: admin")
            return True
        else:
            print("❌ Password verification failed after update")
            return False
    except Exception as e:
        print(f"❌ Error: {e}")
        import traceback
        traceback.print_exc()
        db.rollback()
        return False
    finally:
        db.close()

if __name__ == "__main__":
    success = fix_admin_password()
    sys.exit(0 if success else 1)


