package com.gxwebsoft.cms.controller;

import com.gxwebsoft.cms.entity.CmsMessage;
import com.gxwebsoft.cms.param.CmsMessageParam;
import com.gxwebsoft.cms.service.CmsMessageService;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

/**
 * 留言咨询控制器
 *
 * @author WorkBuddy
 * @since 2026-07-17
 */
@Tag(name = "留言咨询管理")
@RestController
@RequestMapping("/api/cms/cms-message")
public class CmsMessageController extends BaseController {
    @Resource
    private CmsMessageService cmsMessageService;

    @Operation(summary = "分页查询留言咨询")
    @GetMapping("/page")
    public ApiResult<PageResult<CmsMessage>> page(CmsMessageParam param) {
        return success(cmsMessageService.pageRel(param));
    }

    @Operation(summary = "查询全部留言咨询")
    @GetMapping()
    public ApiResult<List<CmsMessage>> list(CmsMessageParam param) {
        return success(cmsMessageService.listRel(param));
    }

    @Operation(summary = "根据id查询留言咨询")
    @GetMapping("/{id}")
    public ApiResult<CmsMessage> get(@PathVariable("id") Integer id) {
        return success(cmsMessageService.getByIdRel(id));
    }

    @Operation(summary = "删除留言咨询")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (cmsMessageService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @Operation(summary = "修改留言状态(完成/归档)")
    @PutMapping("/status")
    public ApiResult<?> updateStatus(@RequestBody CmsMessage message) {
        CmsMessage entity = new CmsMessage();
        entity.setMessageId(message.getMessageId());
        entity.setStatus(message.getStatus());
        if (cmsMessageService.updateById(entity)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @Operation(summary = "回复留言")
    @PutMapping("/reply")
    public ApiResult<?> reply(@RequestBody CmsMessage message) {
        CmsMessage entity = new CmsMessage();
        entity.setMessageId(message.getMessageId());
        entity.setReply(message.getReply());
        entity.setReplyTime(LocalDateTime.now());
        entity.setStatus(1);
        if (cmsMessageService.updateById(entity)) {
            return success("回复成功");
        }
        return fail("回复失败");
    }

    @Operation(summary = "统计概览")
    @GetMapping("/stats")
    public ApiResult<Map<String, Object>> stats() {
        Map<String, Object> map = new HashMap<>();
        map.put("total", cmsMessageService.count());
        map.put("pending", cmsMessageService.count(new LambdaQueryWrapper<CmsMessage>().eq(CmsMessage::getStatus, 0)));
        map.put("replied", cmsMessageService.count(new LambdaQueryWrapper<CmsMessage>().eq(CmsMessage::getStatus, 1)));
        map.put("done", cmsMessageService.count(new LambdaQueryWrapper<CmsMessage>().eq(CmsMessage::getStatus, 2)));
        return success(map);
    }
}
