"""
Password Reset Script
Usage: python reset_password.py <email> <new_password>
"""
import sqlite3
import os
import sys
import bcrypt

db_path = os.path.join(os.path.dirname(__file__), 'autonet.db')


def get_password_hash(password: str) -> str:
    return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')


def reset_password(email: str, new_password: str):
    """Reset user password"""
    if not os.path.exists(db_path):
        print(f"Database not found: {db_path}")
        return False

    conn = sqlite3.connect(db_path)
    cursor = conn.cursor()

    # Check if user exists
    cursor.execute("SELECT id, email FROM users WHERE email = ?", (email,))
    user = cursor.fetchone()

    if not user:
        print(f"User not found: {email}")
        conn.close()
        return False

    # Update password
    password_hash = get_password_hash(new_password)
    cursor.execute("UPDATE users SET password_hash = ? WHERE email = ?", (password_hash, email))
    conn.commit()

    print(f"Password reset successful for: {email}")
    print(f"  New password: {new_password}")

    conn.close()
    return True


if __name__ == "__main__":
    if len(sys.argv) < 3:
        print("Usage: python reset_password.py <email> <new_password>")
        print("Example: python reset_password.py test@test.com password123")
        sys.exit(1)

    email = sys.argv[1]
    password = sys.argv[2]
    reset_password(email, password)
