from fastapi import APIRouter, HTTPException, Query
from decimal import Decimal
from typing import Dict, Any, List, Optional
from app.modules.trial_balance.service import fetch_trial_balance

router = APIRouter(prefix="/trial-balance", tags=["試算表"])

@router.get("", summary="試算表（全科目）")
def get_trial_balance(
    fiscal_year: Optional[int] = Query(None, description="会計年度（例: 2025）"),
    date_from: Optional[str] = Query(None, description="YYYY-MM-DD"),
    date_to: Optional[str] = Query(None, description="YYYY-MM-DD"),
):
    # 👉 没传参数时，给默认值（开发期非常推荐）
    if fiscal_year is None:
        fiscal_year = 2025
    if date_from is None:
        date_from = "2025-01-01"
    if date_to is None:
        date_to = "2025-12-31"

    if date_from > date_to:
        raise HTTPException(status_code=400, detail="期間指定が不正です。")

    result = fetch_trial_balance(date_from, date_to)
    return result
