from fastapi import FastAPI
from fastapi.responses import FileResponse
from app.api.endpoints import users, auth, messages, media
from app.websocket.connection_manager import wsRouter
from fastapi.middleware.cors import CORSMiddleware
import os

app = FastAPI()

app.include_router(auth.authRouter)
app.include_router(users.usersRouter)
app.include_router(messages.messagesRouter)
app.include_router(media.mediaRouter)
app.include_router(wsRouter)

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


@app.get("/check-update")
async def check_update():
    return {
        "latest_version": "2.0.0",
        "apk_url": "https://api.chepuhagram.ru/get-update",
        "force_update": False
    }


@app.get("/get-update")
async def get_image():
    file_path = "app-release.apk"
    if not os.path.exists(file_path):
        return {"error": "Файл не найден"}

    return FileResponse(path=file_path, filename="chepuhagram-release.apk",
                        media_type="application/vnd.android.package-archive",)


@app.head("/get-update")
async def head_image():
    file_path = "app-release.apk"
    if not os.path.exists(file_path):
        return {"error": "Файл не найден"}

    return FileResponse(
        path=file_path,
        filename="chepuhagram-release.apk",
        media_type="application/vnd.android.package-archive"
    )

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