"""Test Logging Utility Manually"""
import sys
import os
import uuid
from datetime import datetime
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from app.database import SessionLocal
from app.utils.logging import log_model_usage
from app.models.user import User


from app.models.job import Job

def test_logging():
    db = SessionLocal()
    try:
        # Get a user
        user = db.query(User).first()
        if not user:
            print("No fake user found, cannot log.")
            return

        print(f"Logging for user: {user.email}")
        
        # Create a dummy Job
        job = Job(
            user_id=user.id,
            module="image_generator",
            action="generate",
            input_data={"prompt": "test"},
            status="completed"
        )
        db.add(job)
        db.commit()
        
        # Log a fake image generation
        log = log_model_usage(
            db=db,
            job_id=str(job.id),
            user_id=str(user.id),
            module="image_generator",
            action="generate",
            provider="openai",
            model="dall-e-3",
            usage_stats={
                "images": 1,
                "processing_time_ms": 4500
            },
            request_metadata={
                "prompt": "A futuristic city with flying cars and neon lights, cyberpunk style",
                "size": "1024x1024",
                "quality": "hd"
            },
            response_metadata={
                "status": "success"
            }
        )
        
        if log:
            print(f"✓ Log created with ID: {log.id}")
            print(f"  Cost: ${log.estimated_cost_usd}")
        else:
            print("✗ Log creation failed (returned None)")
            
    except Exception as e:
        print(f"Error: {e}")
        db.rollback()
    finally:
        db.close()

if __name__ == "__main__":
    test_logging()
