package com.emotion.controller;

import com.emotion.common.PageResult;
import com.emotion.common.Result;
import com.emotion.dto.request.DiaryCommentCreateRequest;
import com.emotion.dto.request.DiaryCommentPageRequest;
import com.emotion.dto.response.DiaryCommentResponse;
import com.emotion.service.DiaryCommentService;
import com.emotion.service.DiaryPostService;
import com.emotion.util.UserContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 日记评论控制器
 *
 * @author huazhongmin
 * @date 2025-07-23
 */
@RestController
@RequestMapping("/diaryComment")
public class DiaryCommentController {

    @Autowired
    private DiaryCommentService diaryCommentService;

    @Autowired
    private DiaryPostService diaryPostService;

    /**
     * 分页查询评论
     */
    @GetMapping(value = "/page")
    public Result<PageResult<DiaryCommentResponse>> getPage(@Validated DiaryCommentPageRequest request) {
        PageResult<DiaryCommentResponse> pageResult = diaryCommentService.getPageWithResponse(request);
        return Result.success(pageResult);
    }

    /**
     * 获取评论树结构
     */
    @GetMapping(value = "/commentTree")
    public Result<List<DiaryCommentResponse>> getCommentTree(@RequestParam String diaryId) {
        List<DiaryCommentResponse> responses = diaryCommentService.getCommentTreeWithResponse(diaryId);
        return Result.success(responses);
    }

    /**
     * 根据ID获取评论详情
     */
    @GetMapping(value = "/detail")
    public Result<DiaryCommentResponse> getById(@RequestParam String id) {
        DiaryCommentResponse response = diaryCommentService.getCommentResponseById(id);
        if (response == null) {
            return Result.notFound("评论不存在");
        }
        return Result.success(response);
    }

    /**
     * 创建评论
     */
    @PostMapping(value = "/create")
    public Result<DiaryCommentResponse> create(@Valid @RequestBody DiaryCommentCreateRequest request) {
        // 从上下文中获取当前用户ID，而不是直接使用请求中的用户ID
        String currentUserId = UserContextUtils.getCurrentUserId();
        if (currentUserId != null) {
            request.setUserId(currentUserId);
        }
        DiaryCommentResponse response = diaryCommentService.createCommentWithResponse(request);
        
        // 更新日记的评论数
        diaryPostService.incrementCommentCount(request.getDiaryId());
        diaryPostService.updateLastCommentTime(request.getDiaryId());
        
        return Result.success(response);
    }

    /**
     * 更新评论
     */
    @PutMapping(value = "/update")
    public Result<DiaryCommentResponse> update(@Valid @RequestBody DiaryCommentCreateRequest request) {
        DiaryCommentResponse response = diaryCommentService.updateCommentWithResponse(request);
        return Result.success(response);
    }

    /**
     * 删除评论
     */
    @DeleteMapping(value = "/delete")
    public Result<Void> delete(@RequestParam String id) {
        boolean deleted = diaryCommentService.deleteComment(id);
        if (!deleted) {
            return Result.error("删除失败");
        }
        
        // 更新日记的评论数
        DiaryCommentResponse response = diaryCommentService.getCommentResponseById(id);
        if (response != null) {
            diaryPostService.decrementCommentCount(response.getDiaryId());
        }
        
        return Result.success();
    }

    /**
     * 软删除评论
     */
    @DeleteMapping(value = "/softDelete")
    public Result<Void> softDelete(@RequestParam String id) {
        boolean deleted = diaryCommentService.softDeleteComment(id);
        if (!deleted) {
            return Result.error("删除失败");
        }
        
        // 更新日记的评论数
        DiaryCommentResponse response = diaryCommentService.getCommentResponseById(id);
        if (response != null) {
            diaryPostService.decrementCommentCount(response.getDiaryId());
        }
        
        return Result.success();
    }

    /**
     * 恢复评论
     */
    @PutMapping(value = "/restore")
    public Result<Void> restore(@RequestParam String id) {
        boolean restored = diaryCommentService.restoreComment(id);
        if (!restored) {
            return Result.error("恢复失败");
        }
        
        // 更新日记的评论数
        DiaryCommentResponse response = diaryCommentService.getCommentResponseById(id);
        if (response != null) {
            diaryPostService.incrementCommentCount(response.getDiaryId());
        }
        
        return Result.success();
    }

    /**
     * 点赞评论
     */
    @PostMapping(value = "/like")
    public Result<Void> like(@RequestParam String id) {
        boolean liked = diaryCommentService.incrementLikeCount(id);
        if (!liked) {
            return Result.error("点赞失败");
        }
        return Result.success();
    }

    /**
     * 取消点赞评论
     */
    @DeleteMapping(value = "/unlike")
    public Result<Void> unlike(@RequestParam String id) {
        boolean unliked = diaryCommentService.decrementLikeCount(id);
        if (!unliked) {
            return Result.error("取消点赞失败");
        }
        return Result.success();
    }

    /**
     * 设置置顶状态
     */
    @PutMapping(value = "/setTop")
    public Result<Void> setTop(@RequestParam String id, @RequestParam Integer isTop) {
        boolean set = diaryCommentService.setTop(id, isTop);
        if (!set) {
            return Result.error("设置置顶状态失败");
        }
        return Result.success();
    }
}