#!/usr/bin/env python3
"""Reset a user's password in the Venture Fund CRM."""

import sys
import os
import getpass

sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'backend'))
from server import get_db, hash_password

def main():
    print("\n  Reset User Password")
    print("  " + "=" * 30 + "\n")

    username = input("  Username: ").strip()
    if not username:
        print("  Error: Username required")
        sys.exit(1)

    conn = get_db()
    user = conn.execute("SELECT id, full_name FROM users WHERE username = ?", (username,)).fetchone()
    if not user:
        print(f"  Error: User '{username}' not found")
        conn.close()
        sys.exit(1)

    print(f"  Found user: {user['full_name']}")

    password = getpass.getpass("  New Password: ")
    if len(password) < 6:
        print("  Error: Password must be at least 6 characters")
        sys.exit(1)

    confirm = getpass.getpass("  Confirm Password: ")
    if password != confirm:
        print("  Error: Passwords don't match")
        sys.exit(1)

    conn.execute("UPDATE users SET password_hash = ? WHERE id = ?",
                 (hash_password(password), user['id']))
    conn.commit()
    conn.close()

    print(f"\n  Password reset successfully for '{username}'!\n")

if __name__ == "__main__":
    main()
