import json
import logging

from fastapi import APIRouter, HTTPException

from app.schemas.brand_dna import BrandDnaModel, GenerateBrandModel
from app.schemas.response_template import ResponseModel
from app.service.brand_dna.service import BrandDna
from app.service.brand_dna.service_generate_brand_info import GenerateBrandInfo

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


@router.post("/seg_product")
def seg_product(request_item: BrandDnaModel):
    """
        创建一个具有以下参数的请求体:
        - **image_url**: 提取图片url
        - **is_brand_dna**: 是否提取属性

        示例参数：
        {
          "image_url": "aida-results/result_00006a48-e315-11ee-b7c8-b48351119060.png",
          "is_brand_dna": false
        }
    """
    try:
        logger.info(f"brand dna request item is : @@@@@@:{json.dumps(request_item.dict())}")
        service = BrandDna(request_item)
        result_url = service.get_result()
    except Exception as e:
        logger.warning(f"brand dna Run Exception @@@@@@:{e}")
        raise HTTPException(status_code=404, detail=str(e))
    return ResponseModel(data=result_url)


@router.post("/GenerateBrand")
def GenerateBrand(request_data: GenerateBrandModel):
    """
        通过prompt 生成 brand name ，brand slogan , brand logo。
        创建一个具有以下参数的请求体:
        - **prompt**:

        示例参数：
        {
          "prompt": "xiaomi",
          "user_id": "89"
        }
    """
    try:
        logger.info(f"GenerateBrand request item is : @@@@@@:{request_data}")
        service = GenerateBrandInfo(request_data)
        data = service.get_result()
        logger.info(f"GenerateBrand response @@@@@@:{data}")
    except Exception as e:
        logger.warning(f"GenerateBrand Run Exception @@@@@@:{e}")
        raise HTTPException(status_code=404, detail=str(e))
    return ResponseModel(data=data)
