package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.CommonPushTask;
import shop.alien.entity.store.vo.CommonPushFunnelVo;
import shop.alien.entity.store.vo.CommonPushReportVo;
import shop.alien.entity.store.vo.CommonPushTaskStatisticsPageVo;
import shop.alien.store.dto.CommonPushSendResultDto;
import shop.alien.store.dto.CommonPushTestSendDto;
import shop.alien.store.service.CommonPushTaskService;

import java.util.Date;

@Api(tags = {"推送任务管理"})
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/commonPushTask")
@RequiredArgsConstructor
public class CommonPushTaskController {

    private final CommonPushTaskService commonPushTaskService;

    /**
     * 新增推送任务入口。
     * <p>调用链：validateTaskForAdd → fillTaskDefaults → auditPushContent → save → savePushReview
     * →（sendType=1 时）doSendTask → commonPushSendService.send</p>
     */
    @ApiOperation("新增推送任务")
    @ApiOperationSupport(order = 1)
    @PostMapping("/add")
    public R<String> add(@RequestBody CommonPushTask task) {
        return commonPushTaskService.add(task);
    }

    @ApiOperation("保存草稿")
    @ApiOperationSupport(order = 2)
    @PostMapping("/saveDraft")
    public R<Long> saveDraft(@RequestBody CommonPushTask task) {
        return commonPushTaskService.saveDraft(task);
    }

    @ApiOperation("根据主键删除（逻辑删除）")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Long", paramType = "query", required = true)
    })
    @GetMapping("/deleteById")
    public R<String> deleteById(@RequestParam Long id) {
        return commonPushTaskService.deleteById(id);
    }

    @ApiOperation("更新推送任务")
    @ApiOperationSupport(order = 4)
    @PostMapping("/update")
    public R<String> update(@RequestBody CommonPushTask task) {
        return commonPushTaskService.update(task);
    }

    @ApiOperation("根据主键查询")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Long", paramType = "query", required = true)
    })
    @GetMapping("/getById")
    public R<CommonPushTask> getById(@RequestParam Long id) {
        return commonPushTaskService.getInfoById(id);
    }

    @ApiOperation("分页列表")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "taskNo", value = "任务编号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "title", value = "标题（模糊）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "任务状态：0-草稿 1-待审核 2-已发送 3-已送达 4-失败 5-审核通过 6-驳回", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pushType", value = "推送类型", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/list")
    public R<IPage<CommonPushTask>> list(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam(required = false) String taskNo,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) Integer pushType) {
        return commonPushTaskService.list(pageNum, pageSize, taskNo, title, status, pushType);
    }

    @ApiOperation("推送统计列表（含概览指标）")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "taskNo", value = "任务编号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "keyword", value = "搜索标题或编号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "任务状态：0-草稿 1-待审核 2-已发送 3-已送达 4-失败 5-审核通过 6-驳回", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pushType", value = "推送类型：1-交易类 2-系统类 3-运营类 4-社交类", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "channel", value = "推送渠道，如 notification/inapp", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间（传日期包含当天 00:00:00 起）", dataType = "Date", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间（传日期包含当天 23:59:59 止）", dataType = "Date", paramType = "query")
    })
    @GetMapping("/statisticsList")
    public R<CommonPushTaskStatisticsPageVo> statisticsList(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam(required = false) String taskNo,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) Integer pushType,
            @RequestParam(required = false) String channel,
            @RequestParam(required = false) Date startTime,
            @RequestParam(required = false) Date endTime) {
        return commonPushTaskService.statisticsList(pageNum, pageSize, taskNo, keyword, status, pushType, channel, startTime, endTime);
    }

    @ApiOperation("推送漏斗统计")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phoneType", value = "机型：1-OPPO 2-VIVO 3-华为 4-苹果 5-荣耀 6-小米 7-三星，筛选 common_push_task_num.phone_type，为空则统计全部", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", dataType = "Date", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", dataType = "Date", paramType = "query")
    })
    @GetMapping("/funnel")
    public R<CommonPushFunnelVo> funnel(
            @RequestParam(required = false) String phoneType,
            @RequestParam(required = false) Date startTime,
            @RequestParam(required = false) Date endTime) {
        return commonPushTaskService.funnel(phoneType, startTime, endTime);
    }

    @ApiOperation("发送测试推送")
    @ApiOperationSupport(order = 9)
    @PostMapping("/sendTest")
    public R<CommonPushSendResultDto> sendTest(@RequestBody CommonPushTestSendDto dto) {
        return commonPushTaskService.sendTest(dto);
    }

    @ApiOperation(value = "报表中心（日报/周报/月报）", notes = "概览与 TOP 榜单均基于 common_push_task_num 汇总")
    @ApiOperationSupport(order = 8)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reportType", value = "报表类型：1-日报 2-周报 3-月报", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/report")
    public R<CommonPushReportVo> report(@RequestParam(required = false) Integer reportType) {
        return commonPushTaskService.report(reportType);
    }
}
