package com.cyksj.web.controller.mini;

import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.model.entity.MiniPopularizeUser;
import com.cyksj.model.entity.MiniPopularizeUserConversationChat;
import com.cyksj.model.request.MiniAiPicturesReq;
import com.cyksj.model.request.MiniPopularizeIncrTimesReq;
import com.cyksj.model.response.MiniPopularizeResp;
import com.cyksj.model.views.MiniPopularizeConfigFrontView;
import com.cyksj.model.views.MiniPopularizeUserConversationImgRecordsView;
import com.cyksj.service.mini.MiniPopularizeFrontService;
import com.cyksj.web.util.StpUserUtil;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.param.Operator;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * server/gpt推广
 * 项目名: yhlxj2
 * 文件名: PopularizeMiniController
 * 创建者: JavaZou
 * 创建时间:2025/4/7 16:01
 */
@RestController
@RequestMapping("/applets/mini/popularize")
@RequiredArgsConstructor
@Slf4j
public class PopularizeMiniController {
	private final BeanSearcher beanSearcher;

	private final HttpServletRequest request;

	private final MiniPopularizeFrontService miniPopularizeFrontService;

	/**
	 * 推广小程序 配置
	 */
	@GetMapping("/get/config")
	public Result<List<MiniPopularizeConfigFrontView>> getConfig() {
		List<MiniPopularizeConfigFrontView> miniPopularizeConfigFrontViews = beanSearcher.searchAll(MiniPopularizeConfigFrontView.class, MapUtils.flatBuilder(request.getParameterMap()).build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(miniPopularizeConfigFrontViews);
	}

	/**
	 * 创作 图片
	 */
	@PostMapping("/create/pictures")
	public Result<MiniPopularizeResp> createPictures(@RequestBody MiniAiPicturesReq miniAiPicturesReq) throws Exception {
		long userId = StpUserUtil.getLoginIdAsLong();
		miniAiPicturesReq.setUserId(userId);
		MiniPopularizeResp resp = miniPopularizeFrontService.createPictures(miniAiPicturesReq);
		return GatewayResponse.SUCCESS.newBuilder().toResult(resp);
	}

	/**
	 * 创作记录
	 */
	@GetMapping("/get/chat")
	public Result<SearchResult<MiniPopularizeUserConversationImgRecordsView>> getChat() throws IOException {
		long userId = StpUserUtil.getLoginIdAsLong();
		SearchResult<MiniPopularizeUserConversationImgRecordsView> search = beanSearcher.search(MiniPopularizeUserConversationImgRecordsView.class, MapUtils.flatBuilder(request.getParameterMap()).field(MiniPopularizeUserConversationImgRecordsView::getUserId, userId).build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}

	/**
	 * 个人作品
	 */
	@GetMapping("/get/personal")
	public Result<SearchResult<MiniPopularizeUserConversationChat>> getPersonal() throws IOException {
		long userId = StpUserUtil.getLoginIdAsLong();
		SearchResult<MiniPopularizeUserConversationChat> search = beanSearcher.search(MiniPopularizeUserConversationChat.class, MapUtils.flatBuilder(request.getParameterMap())
				.field(MiniPopularizeUserConversationChat::getUserId, userId)
				.field(MiniPopularizeUserConversationChat::getRole, MiniPopularizeUserConversationChat.Role.user.name()).op(Operator.NotEqual)
				.field(MiniPopularizeUserConversationChat::getType, MiniPopularizeUserConversationChat.Type.image.name())
				.orderBy(MiniPopularizeUserConversationChat::getId).desc().build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}

	/**
	 * 推广小程序用户信息
	 */
	@GetMapping("/get/user")
	public Result<MiniPopularizeUser> getUser() {
		long userId = StpUserUtil.getLoginIdAsLong();
		MiniPopularizeUser user = miniPopularizeFrontService.getUser(userId);
		return GatewayResponse.SUCCESS.newBuilder().toResult(user);
	}

	/**
	 * 图片生成结果
	 */
	@GetMapping("/get/result/{imageId}")
	public Result<MiniPopularizeUserConversationChat> getImgResultById(@PathVariable Long imageId) {
		long userId = StpUserUtil.getLoginIdAsLong();
		MiniPopularizeUserConversationChat chat = miniPopularizeFrontService.getImgResultById(userId, imageId);
		return GatewayResponse.SUCCESS.newBuilder().toResult(chat);
	}

	/**
	 * 看广告增加画图次数
	 */
	@PostMapping("/incr/times")
	public Result<String> incrGptTimes(@RequestBody MiniPopularizeIncrTimesReq reqs) throws Exception {
		long userId = StpUserUtil.getLoginIdAsLong();
		reqs.setUserId(userId);
		miniPopularizeFrontService.incrGptTimes(reqs);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 关闭新用户弹窗
	 * @return
	 */
	@PutMapping("/close/tc")
	public Result<String> closeTc() {
		long userId = StpUserUtil.getLoginIdAsLong();
		miniPopularizeFrontService.closeTc(userId);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}
}
