#!/usr/bin/python
# -*- coding: utf-8 -*-
# @version        : 1.0
# @Create Time    : 2025/04/03 10:25
# @File           : views.py
# @IDE            : PyCharm
# @desc           : 路由,项目信息管理,包括项目主体,项目图片,项目标签和图片的标注信息

from core.dependencies import IdList
from . import params, schemas, crud, models
from utils.response import SuccessResponse, ErrorResponse

from fastapi import APIRouter, Depends, UploadFile, Form
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_info(
        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/{project_id}", summary="查询标签列表")
async def label_list(
        project_id: int,
        auth: Auth = Depends(FullAdminAuth())
):
    result = await crud.ProjectLabelDal(auth.db).get_datas(v_where=[models.ProjectLabel.project_id == project_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)
    await crud.ProjectImgLabelDal(auth.db).del_img_label(label_ids.ids)
    return SuccessResponse(msg="删除成功")


@app.get("/img", summary="获取图片列表,分两种情况,一个带分页的,一个不带分页的")
async def project_pager(
        param: params.ProjectImageParams = Depends(),
        auth: Auth = Depends(FullAdminAuth())
):
    if param.limit > 0:
        # 分页查询,关联一个标签数量
        datas, count = await crud.ProjectImageDal(auth.db).img_page(param)
        return SuccessResponse(data=datas, count=count)
    else:
        # 直接查询
        datas = await crud.ProjectImageDal(auth.db).get_datas(v_schema=schemas.ProjectImage, **param.dict())
        return SuccessResponse(data=datas)


@app.post("/img", summary="上传图片")
async def up_img(
        project_id: int = Form(...),
        files: list[UploadFile] = Form(...),
        img_type: str = Form(...),
        auth: Auth = Depends(FullAdminAuth())
):
    pro = await crud.ProjectInfoDal(auth.db).get_data(data_id=project_id)
    if pro is None:
        return ErrorResponse(msg="项目查询失败,请稍后再试")
    count = await crud.ProjectImageDal(auth.db).upload_imgs(files=files, pro=pro, img_type=img_type)
    if count > 0:
        return SuccessResponse(msg="上传成功")


@app.delete("/img", summary="删除照片")
async def del_img(
        img_id: IdList = Depends(),
        auth: Auth = Depends(FullAdminAuth())
):
    await crud.ProjectImageDal(auth.db).del_img(img_id.ids)
    return SuccessResponse(msg="删除成功")


@app.post("/img_label", summary="保存图片的标注信息")
async def add_img_label(
        img_label_in: schemas.ProjectImgLeaferLabel,
        auth: Auth = Depends(FullAdminAuth())
):
    await crud.ProjectImgLabelDal(auth.db).add_img_label(img_label_in)
    await crud.ProjectImgLeaferDal(auth.db).add_leafer(img_label_in)
    return SuccessResponse(msg="保存成功")


@app.get("/img_label/{image_id}", summary="获取图片的标注信息")
async def get_img_label(
        image_id: int,
        auth: Auth = Depends(FullAdminAuth())
):
    leafer = await crud.ProjectImgLeaferDal(auth.db).get_leafer(image_id)
    return SuccessResponse(data=leafer)