import os
import json
import sys

# Add current directory to path
sys.path.append(os.getcwd())

import db

def test():
    query = """
    SELECT o.id, 
           GROUP_CONCAT(JSON_OBJECT('id', f.id, 'filename', f.filename, 'file_path', f.file_path, 'file_size', f.file_size, 'quantity', f.quantity, 'preview_path', f.preview_path, 'print_time', f.print_time, 'filament_g', f.filament_g)) as files
    FROM orders o
    LEFT JOIN order_files f ON o.id = f.order_id
    GROUP BY o.id
    ORDER BY o.created_at DESC
    LIMIT 2
    """
    results = db.execute_query(query)
    for row in results:
        print(f"Order ID: {row['id']}")
        if row['files']:
            try:
                files_json = '[' + row['files'] + ']'
                files = json.loads(files_json)
                for f in files:
                    print(f"  File: {f.get('filename')}, ID: {f.get('id')}")
            except Exception as e:
                print(f"  Error parsing JSON: {e}")
        else:
            print("  No files")

if __name__ == "__main__":
    test()
