package com.pig4cloud.pig.marketing.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.marketing.api.dto.MktMgmtPushRecordQueryDTO;
import com.pig4cloud.pig.marketing.api.dto.MktMgmtPushRecordSaveDTO;
import com.pig4cloud.pig.marketing.api.entity.MktMgmtPushRecord;
import com.pig4cloud.pig.marketing.api.vo.rule.StatKeywordVO;
import com.pig4cloud.pig.marketing.service.MktMgmtPushRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: wcl
 * @date: 2025-08-31
 * @description: 营销推送记录
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/push")
@Tag(description = "marketingPush", name = "营销推送记录")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class MktMgmtPushRecordController {

	private final MktMgmtPushRecordService mktMgmtPushRecordService;

	/**
	 * 分页查询营销推送记录
	 * @param queryDTO 查询条件
	 * @return 分页结果
	 */
	@PostMapping("/page")
	@Operation(summary = "分页查询营销推送记录", description = "根据条件分页查询营销推送记录")
	public R<Page<MktMgmtPushRecord>> pageQuery(
			@Parameter(description = "查询条件") @RequestBody @Valid MktMgmtPushRecordQueryDTO queryDTO) {
		log.info("分页查询营销推送记录，查询条件：{}", queryDTO);
		
		try {
			Page<MktMgmtPushRecord> result = mktMgmtPushRecordService.pageQuery(queryDTO);
			return R.ok(result);
		} catch (Exception e) {
			log.error("分页查询营销推送记录异常", e);
			return R.failed("查询失败：" + e.getMessage());
		}
	}

	/**
	 * 新增营销推送记录
	 * @param saveDTO 新增数据
	 * @return 操作结果
	 */
	@PostMapping("/save")
	@Operation(summary = "新增营销推送记录")
	public R<String> saveRecord(
			@Parameter(description = "新增数据") @RequestBody @Valid MktMgmtPushRecordSaveDTO saveDTO) {
		log.info("新增营销推送记录，数据：{}", saveDTO);
		
		try {
			String result = mktMgmtPushRecordService.saveRecord(saveDTO);
			if ("新增成功".equals(result)) {
				return R.ok(result, "新增成功");
			} else {
				return R.failed(result);
			}
		} catch (Exception e) {
			log.error("新增营销推送记录异常", e);
			return R.failed("新增失败：" + e.getMessage());
		}
	}



	@GetMapping("/stat/keyword")
	@Operation(summary = "统计关键词频率")
	public R<List<StatKeywordVO>> statKeyword(@NotNull(message = "统计天数不能为空") @ParameterObject Integer  days) {
		List<StatKeywordVO> result = mktMgmtPushRecordService.statKeyword(days);
		return R.ok(result);
	}
}
