from fastapi import FastAPI, UploadFile, File, HTTPException, APIRouter
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
import os
import pdfplumber
import asyncio
import edge_tts
from datetime import datetime
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()
router = APIRouter(prefix="/api/audio-converter")

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

AUDIO_FOLDER = "./audios"
os.makedirs(AUDIO_FOLDER, exist_ok=True)

def read_pdf_text(pdf_file_path):
    text = ""
    with pdfplumber.open(pdf_file_path) as pdf:
        for page in pdf.pages:
            page_text = page.extract_text()
            if page_text:
                lines = [line.strip() for line in page_text.split('\n') if line.strip()]
                cleaned = ""
                for j, line in enumerate(lines):
                    cleaned += line
                    if not line.endswith(('.', '!', '?')) and j < len(lines) - 1:
                        cleaned += " "
                    else:
                        cleaned += "\n"
                text += cleaned + "\n"
    return text.strip()

async def convert_text_to_speech(text, output_path):
    communicate = edge_tts.Communicate(text, voice="vi-VN-HoaiMyNeural")
    await communicate.save(output_path)

@router.post("/convert/")
async def convert_pdf_to_audio(file: UploadFile = File(...)):
    if file.content_type != "application/pdf":
        raise HTTPException(status_code=400, detail="File không phải PDF")

    tmp_pdf_path = f"tmp_{datetime.now().timestamp()}.pdf"
    with open(tmp_pdf_path, "wb") as f:
        f.write(await file.read())

    text = read_pdf_text(tmp_pdf_path)
    os.remove(tmp_pdf_path)

    if not text.strip():
        raise HTTPException(status_code=400, detail="Không tìm thấy nội dung văn bản trong PDF")

    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    output_path = os.path.join(AUDIO_FOLDER, f"audio_{timestamp}.mp3")

    await convert_text_to_speech(text, output_path)

    return {"filename": os.path.basename(output_path), "message": "Chuyển đổi thành công"}

@router.get("/list-audio/")
def list_audio_files():
    files = [f for f in os.listdir(AUDIO_FOLDER) if f.lower().endswith(".mp3")]
    files.sort()
    return {"files": files}

@router.get("/audio/{filename}")
def get_audio_file(filename: str):
    file_path = os.path.join(AUDIO_FOLDER, filename)
    if not os.path.exists(file_path):
        raise HTTPException(status_code=404, detail="File không tồn tại")
    return FileResponse(file_path, media_type="audio/mpeg")

# Mount static to serve audios if needed
router.mount("/audios", StaticFiles(directory=AUDIO_FOLDER), name="audios")
app.include_router(router)