#!/usr/bin/env python3
import pymysql
import uuid
from datetime import datetime

DB = dict(host='localhost', port=3306, database='xygbfpsg_loans',
          user='xygbfpsg_graz', password='j.ez-xy6##y.rllB',
          charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)

# Hardcoded test user data
USER_ID = str(uuid.uuid4())
USERNAME = 'testuser'
PASSWORD = 'pbkdf2_sha256$870000$8EJ3LKxF2C5qI0MZoJ0J8C$pLVFh8VgZ5h2vH7V0KQ3cOo9F6YtJ5J7J8J9J0J1J2J='  # Test@123
EMAIL = 'testuser@grazuri.com'
FIRST_NAME = 'Test'
LAST_NAME = 'User'
PHONE = '0712345678'
ROLE = 'borrower'
ID_NUMBER = '12345678'

print("Creating test user...")

conn = pymysql.connect(**DB)
try:
    with conn.cursor() as cursor:
        # Check if user exists
        cursor.execute("SELECT id FROM users WHERE username = %s", (USERNAME,))
        existing = cursor.fetchone()
        
        if existing:
            print(f"User '{USERNAME}' exists. Updating password...")
            cursor.execute("UPDATE users SET password = %s WHERE username = %s", (PASSWORD, USERNAME))
            print(f"✅ Password reset")
        else:
            print(f"Creating new user '{USERNAME}'...")
            sql = """
            INSERT INTO users (id, username, password, email, first_name, last_name, 
                             phone_number, role, id_number, is_active, is_staff, is_superuser,
                             date_joined, last_login)
            VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, 1, 0, 0, %s, NULL)
            """
            cursor.execute(sql, (USER_ID, USERNAME, PASSWORD, EMAIL, FIRST_NAME, LAST_NAME,
                               PHONE, ROLE, ID_NUMBER, datetime.now()))
            print(f"✅ User created")
    
    conn.commit()
    
    print("\n" + "="*60)
    print("✅ SUCCESS!")
    print("="*60)
    print(f"Username: {USERNAME}")
    print(f"Password: Test@123")
    print(f"URL: https://grazuri.uzuriapps.xyz/accounts/login/")
    print("="*60)
    
finally:
    conn.close()
