"""提示词模板控制器。"""

from fastapi import Depends, Query
from fastapi.responses import JSONResponse

from fastapi_common.fastapi_common_security.security import verify_access_token
from fastapi_common.fastapi_common_web.controller import BaseController
from fastapi_modules.fastapi_leaudit.domian.Dto.promptTemplateDto import PromptTemplateCreateDTO, PromptTemplateUpdateDTO
from fastapi_modules.fastapi_leaudit.services.promptTemplateService import IPromptTemplateService
from fastapi_modules.fastapi_leaudit.services.impl.promptTemplateServiceImpl import PromptTemplateServiceImpl


class PromptTemplateController(BaseController):
    def __init__(self):
        super().__init__(prefix='/v3/prompt-templates', tags=['提示词模板'])
        self.Service: IPromptTemplateService = PromptTemplateServiceImpl()

        @self.router.get('')
        async def ListPromptTemplates(
            search: str | None = Query(None, description='名称/编码搜索'),
            template_type: str | None = Query(None, description='模板类型，逗号分隔'),
            status: int | None = Query(None, description='状态'),
            page: int = Query(1, ge=1),
            page_size: int = Query(20, ge=1, le=500),
            payload: dict = Depends(verify_access_token),
        ):
            _ = payload
            template_types = [item.strip() for item in str(template_type or '').split(',') if item.strip()]
            data = await self.Service.ListTemplates(search, template_types or None, status, page, page_size)
            return JSONResponse(status_code=200, content={'code': 200, 'message': 'ok', 'data': data.model_dump()})

        @self.router.get('/types')
        async def GetPromptTemplateTypes(payload: dict = Depends(verify_access_token)):
            _ = payload
            data = await self.Service.GetTemplateTypes()
            return JSONResponse(status_code=200, content={'code': 200, 'message': 'ok', 'data': data.model_dump()})

        @self.router.get('/{TemplateId}')
        async def GetPromptTemplate(TemplateId: int, payload: dict = Depends(verify_access_token)):
            _ = payload
            data = await self.Service.GetTemplate(TemplateId)
            return JSONResponse(status_code=200, content={'code': 200, 'message': 'ok', 'data': data.model_dump()})

        @self.router.post('')
        async def CreatePromptTemplate(body: PromptTemplateCreateDTO, payload: dict = Depends(verify_access_token)):
            _ = payload
            data = await self.Service.CreateTemplate(body)
            return JSONResponse(status_code=200, content={'code': 200, 'message': 'ok', 'data': data.model_dump()})

        @self.router.put('/{TemplateId}')
        async def UpdatePromptTemplate(TemplateId: int, body: PromptTemplateUpdateDTO, payload: dict = Depends(verify_access_token)):
            _ = payload
            data = await self.Service.UpdateTemplate(TemplateId, body)
            return JSONResponse(status_code=200, content={'code': 200, 'message': 'ok', 'data': data.model_dump()})

        @self.router.delete('/{TemplateId}')
        async def DeletePromptTemplate(TemplateId: int, payload: dict = Depends(verify_access_token)):
            _ = payload
            await self.Service.DeleteTemplate(TemplateId)
            return JSONResponse(status_code=200, content={'code': 200, 'message': 'ok', 'data': True})

        @self.router.post('/{TemplateId}/duplicate')
        async def DuplicatePromptTemplate(TemplateId: int, new_code: str | None = Query(None), payload: dict = Depends(verify_access_token)):
            _ = payload
            data = await self.Service.DuplicateTemplate(TemplateId, new_code)
            return JSONResponse(status_code=200, content={'code': 200, 'message': 'ok', 'data': data.model_dump()})
