import sys
sys.path.insert(0, '/home/shingokuga/.openclaw/workspace/rapport-chantier/src/api')

import tempfile, os
from fastapi import FastAPI, UploadFile, File
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])

@app.post("/debug/upload")
async def debug_upload(audio: UploadFile = File(...)):
    content = await audio.read()
    path = f"/tmp/debug_audio_{len(content)}_{audio.filename or 'unknown'}"
    with open(path, 'wb') as f:
        f.write(content)
    
    import subprocess
    probe = subprocess.run(
        ['ffprobe', '-v', 'quiet', '-show_format', '-show_streams', path],
        capture_output=True, text=True, timeout=5
    )
    
    return {
        "filename": audio.filename,
        "content_type": audio.content_type,
        "size_bytes": len(content),
        "first_16_bytes_hex": content[:16].hex(),
        "ffprobe": probe.stdout[:500] if probe.stdout else probe.stderr[:500],
        "saved_to": path,
    }

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8103)
