import os
import sys

import django

sys.path.append(os.path.join(os.path.dirname(__file__)))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "thrillwiki.settings")
django.setup()

from django.contrib.auth import get_user_model  # noqa: E402

User = get_user_model()


def ensure_admin():
    username = "admin"
    email = "admin@example.com"
    password = "adminpassword"

    if not User.objects.filter(username=username).exists():
        print(f"Creating superuser {username}...")
        User.objects.create_superuser(username=username, email=email, password=password, role="ADMIN")
        print("Superuser created.")
    else:
        print(f"Superuser {username} already exists.")
        u = User.objects.get(username=username)
        if not u.is_staff or not u.is_superuser or u.role != "ADMIN":
            u.is_staff = True
            u.is_superuser = True
            u.role = "ADMIN"
            u.save()
            print("Updated existing user to ADMIN/Superuser.")


if __name__ == "__main__":
    ensure_admin()
