#!/usr/bin/env python3
"""
Tool_OCR - Create Test User
Creates a test user for API testing
"""

import sys
from pathlib import Path

# Add backend to path
sys.path.insert(0, str(Path(__file__).parent))

from app.core.database import SessionLocal
from app.core.security import get_password_hash
from app.models.user import User


def create_test_user(
    username: str = "admin",
    email: str = "admin@example.com",
    password: str = "admin123",
    full_name: str = "Admin User",
    is_admin: bool = True
):
    """
    Create test user

    Args:
        username: Username
        email: Email address
        password: Plain password (will be hashed)
        full_name: Full name
        is_admin: Is admin user
    """
    db = SessionLocal()

    try:
        # Check if user already exists
        existing_user = db.query(User).filter(User.username == username).first()
        if existing_user:
            print(f"❌ User '{username}' already exists (ID: {existing_user.id})")
            return False

        # Create user
        user = User(
            username=username,
            email=email,
            password_hash=get_password_hash(password),
            full_name=full_name,
            is_active=True,
            is_admin=is_admin
        )

        db.add(user)
        db.commit()
        db.refresh(user)

        print(f"✅ Created user successfully:")
        print(f"   ID: {user.id}")
        print(f"   Username: {user.username}")
        print(f"   Email: {user.email}")
        print(f"   Full Name: {user.full_name}")
        print(f"   Is Admin: {user.is_admin}")
        print(f"   Is Active: {user.is_active}")
        print(f"\n📝 Login credentials:")
        print(f"   Username: {username}")
        print(f"   Password: {password}")

        return True

    except Exception as e:
        print(f"❌ Error creating user: {e}")
        db.rollback()
        return False

    finally:
        db.close()


if __name__ == "__main__":
    print("=" * 60)
    print("Tool_OCR - Create Test User")
    print("=" * 60)

    # Create admin user
    success = create_test_user()

    # Also create a regular test user
    if success:
        print("\n" + "-" * 60)
        create_test_user(
            username="testuser",
            email="test@example.com",
            password="test123",
            full_name="Test User",
            is_admin=False
        )

    print("\n" + "=" * 60)
    print("Done!")
    print("=" * 60)
