#!/usr/bin/env python
"""
Create a test superuser
"""
import os
import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
django.setup()

from users.models import CustomUser, Branch
from django.contrib.auth.hashers import make_password

print("Creating test superuser...")
print("=" * 60)

# Create main branch if it doesn't exist
branch, created = Branch.objects.get_or_create(
    name='Main Branch',
    defaults={
        'code': 'MAIN',
        'address': 'Nairobi, Kenya',
        'phone_number': '+254700000000',
        'email': 'main@branchbusinessadvance.com',
        'is_main_branch': True,
        'is_active': True,
    }
)
if created:
    print(f"✓ Created branch: {branch.name}")
else:
    print(f"- Branch exists: {branch.name}")

# Create superuser
try:
    user = CustomUser.objects.create(
        username='admin',
        email='admin@branchbusinessadvance.com',
        first_name='Admin',
        last_name='User',
        phone_number='+254700000000',
        role='admin',
        is_staff=True,
        is_superuser=True,
        is_active=True,
        branch=branch,
        password=make_password('admin123')  # Change this password!
    )
    print(f"✓ Created superuser: {user.username}")
    print(f"  Email: {user.email}")
    print(f"  Password: admin123")
    print(f"  Role: {user.role}")
except Exception as e:
    print(f"⚠ Error creating superuser: {e}")
    print("\nTrying to get existing admin user...")
    try:
        user = CustomUser.objects.get(username='admin')
        print(f"- Admin user already exists: {user.username}")
    except:
        print("✗ Could not create or find admin user")

print("\n" + "=" * 60)
print("Done!")
print("\nYou can now login at: http://127.0.0.1:8000/login/")
print("Username: admin")
print("Password: admin123")
print("=" * 60)
