import sys
import os
from pathlib import Path

# 将项目根目录添加到 sys.path 以支持直接运行 python app/main.py
root_dir = Path(__file__).resolve().parent.parent
if str(root_dir) not in sys.path:
    sys.path.insert(0, str(root_dir))

from fastapi import FastAPI
{% for table in tables %}
from {{ base_package }}.features.database_basics.{{ table.name }} import create as {{ table.name }}_create
from {{ base_package }}.features.database_basics.{{ table.name }} import get_list as {{ table.name }}_get_list
from {{ base_package }}.features.database_basics.{{ table.name }} import get_by_id as {{ table.name }}_get_by_id
from {{ base_package }}.features.database_basics.{{ table.name }} import update as {{ table.name }}_update
from {{ base_package }}.features.database_basics.{{ table.name }} import delete as {{ table.name }}_delete
{% endfor %}

app = FastAPI(title="CRUD API")

{% for table in tables %}
app.include_router({{ table.name }}_create.router, prefix="/{{ table.name }}", tags=["{{ table.name }}"])
app.include_router({{ table.name }}_get_list.router, prefix="/{{ table.name }}", tags=["{{ table.name }}"])
app.include_router({{ table.name }}_get_by_id.router, prefix="/{{ table.name }}", tags=["{{ table.name }}"])
app.include_router({{ table.name }}_update.router, prefix="/{{ table.name }}", tags=["{{ table.name }}"])
app.include_router({{ table.name }}_delete.router, prefix="/{{ table.name }}", tags=["{{ table.name }}"])
{% endfor %}

@app.get("/")
async def root():
    return {"message": "Welcome to CRUD API"}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run("{{ base_package }}.main:app", host="127.0.0.1", port=8000, reload=True)
