import json
import logging

from fastapi import APIRouter, HTTPException

from app.core.config import DEBUG
from app.schemas.attribute_retrieve import *
from app.schemas.response_template import ResponseModel
from app.service.attribute.config import const, local_debug_const
from app.service.attribute.service_att_recognition import AttributeRecognition
from app.service.attribute.service_category_recognition import CategoryRecognition

router = APIRouter()
logger = logging.getLogger()


# 属性识别
@router.post("/attribute_recognition", response_model=ResponseModel)
def attribute_recognition(request_item: list[AttributeRecognitionModel]):
    """
        获取sketch的属性，collar sleeve_length 等等
        创建一个具有以下参数的请求体:
        - **category**: sketch的类别 ，Dress
        - **colony**: 服装类别，男装或女装
        - **sketch_img_url**: 被提取属性的S3或minio url地址

        示例参数：
        [
            {
                "category": "Dress",
                "colony": "Female",
                "sketch_img_url": "aida-users/89/sketchboard/female/Dress/ae976103-d7ec-4eed-b5d1-3e5f04d8be26.jpg"
            }
        ]
    """
    try:
        for item in request_item:
            logger.debug(f"attribute_recognition request item is : @@@@@@:{json.dumps(item.dict())}")
        if DEBUG:
            service = AttributeRecognition(const=local_debug_const, request_data=request_item)
        else:
            service = AttributeRecognition(const=const, request_data=request_item)
        data = service.get_result()
        logger.debug(f"attribute_recognition response @@@@@@:{json.dumps(data)}")
    except Exception as e:
        logger.warning(f"attribute_recognition Run Exception @@@@@@:{e}")
        raise HTTPException(status_code=404, detail=str(e))
    return ResponseModel(data={"list": data})


# 类别识别
@router.post("/category_recognition")
def category_recognition(request_item: list[CategoryRecognitionModel]):
    """
        获取sketch的类别，dress blouse 等等
        创建一个具有以下参数的请求体:
        - **colony**: 服装类别，male或Female
        - **sketch_img_url**: 被提取sketch类别的S3或minio url地址

        示例参数：
        [
            {
                "colony": "Female",
                "sketch_img_url": "aida-users/89/sketchboard/female/Dress/ae976103-d7ec-4eed-b5d1-3e5f04d8be26.jpg"
            }
        ]
    """
    try:
        for item in request_item:
            logger.info(f"category_recognition request item is : @@@@@@:{json.dumps(item.dict())}")
        service = CategoryRecognition(request_data=request_item)
        data = service.get_result()
        logger.info(f"category_recognition response @@@@@@:{json.dumps(data)}")
    except Exception as e:
        logger.warning(f"category_recognition Run Exception @@@@@@:{e}")
        raise HTTPException(status_code=404, detail=str(e))
    return ResponseModel(data=data)
