from fastapi import APIRouter, HTTPException, Query
from app.modules.general_ledger.service import fetch_general_ledger

router = APIRouter(
    prefix="/general-ledger",
    tags=["元帳"]
)

@router.get("", summary="元帳取得（科目別）")
def get_general_ledger(
    account_id: int = Query(...),
    date_from: str = Query(...),
    date_to: str = Query(...)
):
    if date_from > date_to:
        raise HTTPException(status_code=400, detail="期間指定が不正です。")

    result = fetch_general_ledger(account_id, date_from, date_to)
    if result is None:
        raise HTTPException(status_code=400, detail="科目が存在しません。")

    return {
        "period": {
            "from": date_from,
            "to": date_to
        },
        **result
    }
