#!/usr/bin/python
# -*- coding: utf-8 -*-
# @version        : 1.0
# @Create Time    : 2025/04/03 10:25
# @File           : views.py
# @IDE            : PyCharm
# @desc           : 路由，视图文件
from utils.response import SuccessResponse, ErrorResponse
from . import params, schemas, crud, models
from core.dependencies import IdList

from fastapi import APIRouter, Depends
from apps.vadmin.auth.utils.current import FullAdminAuth
from apps.vadmin.auth.utils.validation.auth import Auth


app = APIRouter()


###########################################################
#    项目信息
###########################################################
@app.get("/list", summary="获取项目信息列表")
async def project_pager(
        param: params.ProjectInfoParams = Depends(),
        auth: Auth = Depends(FullAdminAuth())
):
    datas, count = await crud.ProjectInfoDal(auth.db).get_project_pager(project=param, auth=auth)
    return SuccessResponse(datas, count=count)


@app.post("/info", summary="新建项目")
async def add_project(
        pro_in: schemas.ProjectInfoIn,
        auth: Auth = Depends(FullAdminAuth())
):
    check = await crud.ProjectInfoDal(auth.db).check_name(project_name=pro_in.project_name)
    if check:
        return ErrorResponse(msg="存在相同名称的项目，不能创建")
    result = await crud.ProjectInfoDal(auth.db).add_project(pro_in, auth)
    return SuccessResponse(data=result)


@app.get("/info/{pro_id}", summary="查询项目信息")
async def project(
        pro_id: int,
        auth: Auth = Depends(FullAdminAuth())
):
    result = await crud.ProjectInfoDal(auth.db).get_data(data_id=pro_id, v_schema=schemas.ProjectInfoOut)
    return SuccessResponse(data=result)


@app.delete("/info", summary="删除项目")
async def del_project(
        pro_ids: IdList = Depends(),
        auth: Auth = Depends(FullAdminAuth())
):
    await crud.ProjectInfoDal(auth.db).delete_datas(ids=pro_ids.ids, v_soft=True)
    return SuccessResponse(msg="删除成功")


@app.get("/label/{pro_id}", summary="查询标签列表")
async def label_list(
        pro_id: int,
        auth: Auth = Depends(FullAdminAuth())
):
    result = await crud.ProjectLabelDal(auth.db).get_datas(v_where=[models.ProjectLabel.project_id == pro_id],
                                                           v_schema=schemas.ProjectLabel)
    return SuccessResponse(data=result)


@app.post("/label", summary="新建标签")
async def add_label(
        label_in: schemas.ProjectLabel,
        auth: Auth = Depends(FullAdminAuth())
):
    check = await crud.ProjectLabelDal(auth.db).check_label_name(label_in.label_name, label_in.project_id)
    if check:
        return ErrorResponse(msg="存在相同名称的标签，不能新建")
    await crud.ProjectLabelDal(auth.db).create_data(data=label_in)
    return SuccessResponse(msg="新建成功")


@app.put("/label", summary="修改标签")
async def update_label(
        label_in: schemas.ProjectLabel,
        auth: Auth = Depends(FullAdminAuth())
):
    check = await crud.ProjectLabelDal(auth.db).check_label_name(label_in.label_name, label_in.project_id, label_in.id)
    if check:
        return ErrorResponse(msg="存在相同名称的标签，不能修改")
    await crud.ProjectLabelDal(auth.db).put_data(data_id=label_in.id, data=label_in)
    return SuccessResponse(msg="修改成功")


@app.delete("/label", summary="删除标签")
async def delete_label(
        label_ids: IdList = Depends(),
        auth: Auth = Depends(FullAdminAuth())
):
    await crud.ProjectLabelDal(auth.db).delete_datas(label_ids.ids)
    return SuccessResponse(msg="删除成功")





