from fastapi import Depends, APIRouter
from sqlalchemy.orm import Session
from app import get_db
from app.api.account import schemas, crud
from libs.result_format import HttpResultResponse, HttpMessage

router = APIRouter()


@router.post("/create")
def create_account(data: schemas.AccountCreate, db: Session = Depends(get_db)):
    """添加账户"""
    db_info = crud.get_account(db, name=data.name)
    if db_info:
        return HttpResultResponse(code=400, msg=HttpMessage.ACCOUNT_EXIST)
    res = crud.create_account(db=db, param=data)
    if not res:
        return HttpResultResponse(code=500, msg=res)
    return HttpResultResponse(data=res.id)


@router.post("/list")
def user_list(data: schemas.AccountList, db: Session = Depends(get_db)):
    """账户列表"""
    result = crud.get_account_list(db, data)
    return HttpResultResponse(total=len(result), data=result)


@router.post("/update")
def read_account(data: schemas.AccountUpdate, db: Session = Depends(get_db)):
    """账户修改"""
    crud.update_account_info(db, data)
    return HttpResultResponse()


@router.post("/finance/info/{aid}")
def finance_information(aid, db: Session = Depends(get_db)):
    """财务信息"""
    crud.get_finance_info(db, aid)
    return HttpResultResponse()
