"""Test Logging Filenames"""
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_filenames():
    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": "filename test"},
            status="completed"
        )
        db.add(job)
        db.commit()
        
        # Log a fake image generation with filenames
        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": 2,
                "processing_time_ms": 5000
            },
            request_metadata={
                "prompt": "Two cats playing chess",
                "size": "1024x1024"
            },
            response_metadata={
                "output_assets": ["asset_1", "asset_2"],
                "filenames": ["cat_chess_01.png", "cat_chess_02.png"]
            }
        )
        
        if log:
            print(f"✓ Log created with ID: {log.id}")
            if log.response_metadata.get("filenames"):
                 print(f"  Filenames found: {log.response_metadata['filenames']}")
            else:
                 print("✗ Filenames missing from log!")
        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_filenames()
