package com.emotion.controller;

import com.emotion.common.PageResult;
import com.emotion.common.Result;
import com.emotion.dto.request.ConversationCreateRequest;
import com.emotion.dto.request.ConversationPageRequest;
import com.emotion.dto.response.ConversationResponse;
import com.emotion.service.ConversationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 对话控制器
 * 
 * @author huazhongmin
 * @date 2025-07-23
 */
@RestController
@RequestMapping("/conversation")
public class ConversationController {

    @Autowired
    private ConversationService conversationService;

    /**
     * 分页查询对话
     */
    @GetMapping(value = "/page")
    public Result<PageResult<ConversationResponse>> getPage(@Valid ConversationPageRequest request) {
        PageResult<ConversationResponse> pageResult = conversationService.getPageWithResponse(request);
        return Result.success(pageResult);
    }

    /**
     * 根据ID获取对话
     */
    @GetMapping(value = "/detail")
    public Result<ConversationResponse> getById(@RequestParam String id) {
        ConversationResponse response = conversationService.getConversationResponseById(id);
        if (response == null) {
            return Result.notFound("对话不存在");
        }
        return Result.success(response);
    }

    /**
     * 创建对话
     */
    @PostMapping(value = "/create")
    public Result<ConversationResponse> create(@Valid @RequestBody ConversationCreateRequest request,
            HttpServletRequest httpRequest) {
        ConversationResponse response = conversationService.createConversationWithResponse(request, httpRequest);
        return Result.success(response);
    }

    /**
     * 更新对话
     */
    @PutMapping(value = "/update")
    public Result<ConversationResponse> update(@RequestBody ConversationCreateRequest request) {
        ConversationResponse response = conversationService.updateConversationWithResponse(request);
        return Result.success(response);
    }

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

    /**
     * 获取活跃对话
     */
    @GetMapping(value = "/active")
    public Result<List<ConversationResponse>> getActiveConversations() {
        List<ConversationResponse> responses = conversationService.getActiveConversationsWithResponse();
        return Result.success(responses);
    }

    /**
     * 获取归档对话
     */
    @GetMapping(value = "/archived")
    public Result<List<ConversationResponse>> getArchivedConversations() {
        List<ConversationResponse> responses = conversationService.getArchivedConversationsWithResponse();
        return Result.success(responses);
    }

    /**
     * 更新对话状态
     */
    @PutMapping(value = "/status")
    public Result<Void> updateConversationStatus(
            @RequestParam String id,
            @RequestParam String status) {
        boolean updated = conversationService.updateConversationStatus(id, status);
        if (!updated) {
            return Result.error("更新状态失败");
        }
        return Result.success();
    }
}