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.core.cache.WfChatCache;
import org.springblade.plugin.workflow.chat.excel.WfChatAppExcel;
import org.springblade.plugin.workflow.chat.pojo.entity.WfChatAppEntity;
import org.springblade.plugin.workflow.chat.pojo.vo.WfChatAppVO;
import org.springblade.plugin.workflow.chat.service.IWfChatAppService;
import org.springblade.plugin.workflow.chat.wrapper.WfChatAppWrapper;
import org.springframework.web.bind.annotation.*;

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

/**
 * dify - 应用 控制器
 *
 * @author ssc
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/chat/app")
@Tag(name = "dify - 应用", description = "dify - 应用接口")
public class WfChatAppController extends BladeController {

	private final IWfChatAppService appService;

	/**
	 * dify - 应用 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description = "传入app")
	public R<WfChatAppVO> detail(WfChatAppEntity model) {
		WfChatAppEntity detail = appService.getOne(Condition.getQueryWrapper(model));
		return R.data(WfChatAppWrapper.build().entityVO(detail));
	}

	/**
	 * dify - 应用 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页", description = "传入app")
	public R<IPage<WfChatAppVO>> list(@Parameter(hidden = true) @RequestParam Map<String, Object> model, Query query) {
		IPage<WfChatAppEntity> pages = appService.page(Condition.getPage(query), Condition.getQueryWrapper(model, WfChatAppEntity.class));
		return R.data(WfChatAppWrapper.build().pageVO(pages));
	}

	/**
	 * dify - 应用 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "分页", description = "传入app")
	public R<IPage<WfChatAppVO>> page(WfChatAppVO model, Query query) {
		IPage<WfChatAppVO> pages = appService.selectAppPage(Condition.getPage(query), model);
		return R.data(pages);
	}

	/**
	 * dify - 应用 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "新增", description = "传入app")
	public R<?> save(@Valid @RequestBody WfChatAppEntity model) {
		return R.status(appService.save(model));
	}

	/**
	 * dify - 应用 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "修改", description = "传入app")
	public R<?> update(@Valid @RequestBody WfChatAppEntity model) {
		WfChatCache.clearCache();
		return R.status(appService.updateById(model));
	}

	/**
	 * dify - 应用 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "新增或修改", description = "传入app")
	public R<?> submit(@Valid @RequestBody WfChatAppEntity model) {
		appService.submit(model);
		return R.success("操作成功");
	}

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


	/**
	 * 导出数据
	 */
	@IsAdmin
	@GetMapping("/export-model")
	@ApiOperationSupport(order = 9)
	@Operation(summary = "导出数据", description = "传入app")
	public void exportModel(@Parameter(hidden = true) @RequestParam Map<String, Object> model, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<WfChatAppEntity> queryWrapper = Condition.getQueryWrapper(model, WfChatAppEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(Model::getTenantId, bladeUser.getTenantId());
		//}
		//queryWrapper.lambda().eq(ChatModelEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<WfChatAppExcel> list = appService.exportApp(queryWrapper);
		ExcelUtil.export(response, "dify - 应用数据" + DateUtil.time(), "dify - 应用数据表", list, WfChatAppExcel.class);
	}

}
