package org.springblade.plugin.workflow.chat.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.annotation.IsAdmin;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.plugin.workflow.chat.excel.WfChatAssistantExcel;
import org.springblade.plugin.workflow.chat.pojo.entity.WfChatAssistantEntity;
import org.springblade.plugin.workflow.chat.pojo.vo.WfChatAssistantVO;
import org.springblade.plugin.workflow.chat.service.IWfChatAssistantService;
import org.springblade.plugin.workflow.chat.wrapper.WfChatAssistantWrapper;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * dify - 智能助理 控制器
 *
 * @author ssc
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/chat/assistant")
@Tag(name = "dify - 智能助理", description = "dify - 智能助理接口")
public class WfChatAssistantController extends BladeController {

	private final IWfChatAssistantService assistantService;

	/**
	 * dify - 智能助理 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description = "传入assistant")
	public R<WfChatAssistantVO> detail(WfChatAssistantEntity application) {
		WfChatAssistantEntity detail = assistantService.getOne(Condition.getQueryWrapper(application));
		return R.data(WfChatAssistantWrapper.build().entityVO(detail));
	}

	/**
	 * dify - 智能助理 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页", description = "传入assistant")
	public R<IPage<WfChatAssistantVO>> list(@Parameter(hidden = true) @RequestParam Map<String, Object> application, Query query) {
		IPage<WfChatAssistantEntity> pages = assistantService.page(Condition.getPage(query), Condition.getQueryWrapper(application, WfChatAssistantEntity.class));
		return R.data(WfChatAssistantWrapper.build().pageVO(pages));
	}

	/**
	 * dify - 智能助理 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "分页", description = "传入assistant")
	public R<IPage<WfChatAssistantVO>> page(WfChatAssistantVO application, Query query) {
		IPage<WfChatAssistantVO> pages = assistantService.selectAssistantPage(Condition.getPage(query), application);
		return R.data(pages);
	}

	/**
	 * dify - 智能助理 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "新增", description = "传入assistant")
	public R<?> save(@Valid @RequestBody WfChatAssistantEntity application) {
		return R.status(assistantService.save(application));
	}

	/**
	 * dify - 智能助理 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "修改", description = "传入assistant")
	public R<?> update(@Valid @RequestBody WfChatAssistantEntity application) {
		return R.status(assistantService.updateById(application));
	}

	/**
	 * dify - 智能助理 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "新增或修改", description = "传入assistant")
	public R<?> submit(@Valid @RequestBody WfChatAssistantEntity application) {
		return R.status(assistantService.saveOrUpdate(application));
	}

	/**
	 * dify - 智能助理 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "逻辑删除", description = "传入ids")
	public R<?> remove(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
		return R.status(assistantService.deleteLogic(Func.toLongList(ids)));
	}


	/**
	 * 导出数据
	 */
	@IsAdmin
	@GetMapping("/export-application")
	@ApiOperationSupport(order = 9)
	@Operation(summary = "导出数据", description = "传入assistant")
	public void exportApplication(@Parameter(hidden = true) @RequestParam Map<String, Object> application, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<WfChatAssistantEntity> queryWrapper = Condition.getQueryWrapper(application, WfChatAssistantEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(Application::getTenantId, bladeUser.getTenantId());
		//}
		//queryWrapper.lambda().eq(ChatApplicationEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<WfChatAssistantExcel> list = assistantService.exportAssistant(queryWrapper);
		ExcelUtil.export(response, "dify - 智能助理数据" + DateUtil.time(), "dify - 智能助理数据表", list, WfChatAssistantExcel.class);
	}

}
