package com.emotion.controller;

import com.emotion.common.PageResult;
import com.emotion.common.Result;
import com.emotion.dto.request.MessageCreateRequest;
import com.emotion.dto.request.MessagePageRequest;
import com.emotion.dto.request.MessageSearchRequest;
import com.emotion.dto.request.MessageRecentRequest;
import com.emotion.dto.response.MessageResponse;
import com.emotion.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 消息控制器
 *
 * @author huazhongmin
 * @date 2025-09-08
 */
@RestController
@RequestMapping("/message")
@Slf4j
public class MessageController {

    @Autowired
    private MessageService messageService;

    /**
     * 创建消息
     */
    @PostMapping(value = "/create")
    public Result<MessageResponse> create(@Valid @RequestBody MessageCreateRequest request) {
        MessageResponse response = messageService.createMessageFromRequest(request);
        return Result.success(response);
    }

    /**
     * 根据ID获取消息
     */
    @GetMapping(value = "/detail")
    public Result<MessageResponse> getById(@RequestParam String id) {
        MessageResponse response = messageService.getMessageById(id);
        if (response == null) {
            return Result.notFound("消息不存在");
        }
        return Result.success(response);
    }

    /**
     * 分页查询消息
     */
    @GetMapping(value = "/page")
    public Result<PageResult<MessageResponse>> getPage(@Validated MessagePageRequest request) {
        PageResult<MessageResponse> pageResult = messageService.getPageWithResponse(request);
        return Result.success(pageResult);
    }

    /**
     * 搜索消息
     */
    @PostMapping(value = "/search")
    public Result<PageResult<MessageResponse>> search(@Valid @RequestBody MessageSearchRequest request) {
        PageResult<MessageResponse> pageResult = messageService.searchWithResponse(request);
        return Result.success(pageResult);
    }

    /**
     * 获取最近的消息
     */
    @PostMapping(value = "/recent")
    public Result<PageResult<MessageResponse>> getRecentMessages(@Valid @RequestBody MessageRecentRequest request) {
        PageResult<MessageResponse> pageResult = messageService.getRecentWithResponse(request);
        return Result.success(pageResult);
    }

    /**
     * 更新消息
     */
    @PutMapping(value = "/update")
    public Result<MessageResponse> update(@RequestParam String id, @RequestParam String content) {
        MessageResponse response = messageService.updateMessage(id, content);
        if (response == null) {
            return Result.notFound("消息不存在");
        }
        return Result.success(response);
    }

    /**
     * 删除消息
     */
    @DeleteMapping(value = "/delete")
    public Result<Void> delete(@RequestParam String id) {
        boolean deleted = messageService.deleteMessage(id);
        if (!deleted) {
            return Result.error("删除失败");
        }
        return Result.success();
    }
}