#!/usr/bin/env python3
"""Test script to verify image upload functionality"""
import asyncio
import sys
from pathlib import Path

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

from database import AsyncSessionLocal
from models import User
from sqlalchemy import select
import bcrypt
import jwt
from datetime import datetime, timezone, timedelta

SECRET_KEY = 'techzone-super-secret-key-2024-production'
ALGORITHM = "HS256"

async def test_upload():
    """Test that we can generate a valid admin token"""
    async with AsyncSessionLocal() as db:
        # Find admin user
        result = await db.execute(
            select(User).where(User.email == "admin@techzone.com")
        )
        admin = result.scalar_one_or_none()
        
        if not admin:
            print("❌ Admin user not found!")
            print("Creating admin user...")
            
            hashed_password = bcrypt.hashpw("admin123".encode(), bcrypt.gensalt())
            admin = User(
                email="admin@techzone.com",
                name="Admin",
                password_hash=hashed_password.decode(),
                role="admin"
            )
            db.add(admin)
            await db.commit()
            await db.refresh(admin)
            print("✅ Admin user created")
        
        # Generate token
        expires = datetime.now(timezone.utc) + timedelta(hours=24)
        token_data = {
            "sub": admin.id,
            "email": admin.email,
            "exp": expires
        }
        token = jwt.encode(token_data, SECRET_KEY, algorithm=ALGORITHM)
        
        print("\n" + "="*60)
        print("ADMIN TOKEN FOR TESTING:")
        print("="*60)
        print(token)
        print("="*60)
        print("\nYou can use this token to test image upload:")
        print(f'\ncurl -X POST http://localhost:8181/api/upload/image \\')
        print(f'  -H "Authorization: Bearer {token}" \\')
        print(f'  -F "file=@/path/to/your/image.jpg"')
        print("\n")

if __name__ == "__main__":
    asyncio.run(test_upload())
