#!/usr/bin/env python3
"""
Creates or resets the admin user.
Run once after first deployment:
    python setup_admin.py
"""
import sys
import os
sys.path.insert(0, os.path.dirname(__file__))

from app.database import SessionLocal
from app.models import User
from app.auth import hash_password

USERNAME = "admin"
PASSWORD = "admin123"   # Change after first login!
FULL_NAME = "System Administrator"
ROLE = "OWNER"

db = SessionLocal()
user = db.query(User).filter(User.username == USERNAME).first()
if user:
    user.password_hash = hash_password(PASSWORD)
    user.role = ROLE
    user.is_active = True
    print(f"[OK] Updated existing user '{USERNAME}'")
else:
    db.add(User(
        username=USERNAME,
        password_hash=hash_password(PASSWORD),
        role=ROLE,
        full_name=FULL_NAME,
        is_active=True,
    ))
    print(f"[OK] Created user '{USERNAME}'")

db.commit()
db.close()
print(f"     Username : {USERNAME}")
print(f"     Password : {PASSWORD}")
print(f"     Role     : {ROLE}")
print("\n  !! Change the password after first login !!")
