"""
Dateiverwaltung - Modulares Dokumenten-Management-System
Hauptanwendung mit FastAPI
"""
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse
from fastapi import Request
from pathlib import Path
import logging

from .models import init_db
from .routes.api import router as api_router
from .config import BASE_DIR

# Logging konfigurieren
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)

# App erstellen
app = FastAPI(
    title="Dateiverwaltung",
    description="Modulares Dokumenten-Management-System",
    version="1.0.0"
)

# Statische Dateien
frontend_dir = BASE_DIR / "frontend"
app.mount("/static", StaticFiles(directory=frontend_dir / "static"), name="static")

# Templates
templates = Jinja2Templates(directory=frontend_dir / "templates")

# API Router
app.include_router(api_router)


@app.on_event("startup")
async def startup():
    """Initialisierung beim Start"""
    init_db()
    logging.info("Datenbank initialisiert")


@app.get("/", response_class=HTMLResponse)
async def index(request: Request):
    """Hauptseite"""
    return templates.TemplateResponse("index.html", {"request": request})


@app.get("/health")
async def health():
    """Health Check für Docker"""
    return {"status": "ok"}


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