package com.emotion.controller;

import com.emotion.common.PageResult;
import com.emotion.common.Result;
import com.emotion.dto.request.DiaryPostCreateRequest;
import com.emotion.dto.request.DiaryPostPageRequest;
import com.emotion.dto.request.DiaryPostUpdateRequest;
import com.emotion.dto.response.DiaryPostResponse;
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;

/**
 * 用户日记控制器
 *
 * @author huazhongmin
 * @date 2025-07-23
 */
@RestController
@RequestMapping("/diaryPost")
public class DiaryPostController {

    @Autowired
    private DiaryPostService diaryPostService;

    /**
     * 分页查询日记
     */
    @GetMapping(value = "/page")
    public Result<PageResult<DiaryPostResponse>> getPage(@Validated DiaryPostPageRequest request) {
        return Result.success(diaryPostService.getPageWithResponse(request));
    }

    /**
     * 根据ID获取日记详情
     */
    @GetMapping(value = "/detail")
    public Result<DiaryPostResponse> getById(@RequestParam String id) {
        DiaryPostResponse response = diaryPostService.getDiaryPostResponseById(id);
        if (response == null) {
            return Result.notFound("日记不存在");
        }
        return Result.success(response);
    }

    /**
     * 创建日记
     */
    @PostMapping(value = "/create")
    public Result<DiaryPostResponse> create(@Valid @RequestBody DiaryPostCreateRequest request) {
        // 从上下文中获取当前用户ID，而不是直接使用请求中的用户ID
        String currentUserId = UserContextUtils.getCurrentUserId();
        if (currentUserId != null) {
            request.setUserId(currentUserId);
        }
        return Result.success(diaryPostService.createDiaryPostWithResponse(request));
    }

    /**
     * 发表日记并生成AI评论
     */
    @PostMapping(value = "/publish")
    public Result<DiaryPostResponse> publish(@Valid @RequestBody DiaryPostCreateRequest request) {
        // 从上下文中获取当前用户ID，而不是直接使用请求中的用户ID
        String currentUserId = UserContextUtils.getCurrentUserId();
        if (currentUserId != null) {
            request.setUserId(currentUserId);
        }
        return Result.success(diaryPostService.publishDiaryWithAiComment(request));
    }

    /**
     * 更新日记
     */
    @PutMapping(value = "/update")
    public Result<DiaryPostResponse> update(@Valid @RequestBody DiaryPostUpdateRequest request) {
        DiaryPostResponse response = diaryPostService.updateDiaryPostWithResponse(request);
        if (response == null) {
            return Result.error("更新失败");
        }
        return Result.success(response);
    }

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

    /**
     * 软删除日记
     */
    @DeleteMapping(value = "/softDelete")
    public Result<Void> softDelete(@RequestParam String id) {
        boolean deleted = diaryPostService.softDeleteDiaryPost(id);
        if (!deleted) {
            return Result.error("删除失败");
        }
        return Result.success();
    }

    /**
     * 恢复日记
     */
    @PutMapping(value = "/restore")
    public Result<Void> restore(@RequestParam String id) {
        boolean restored = diaryPostService.restoreDiaryPost(id);
        if (!restored) {
            return Result.error("恢复失败");
        }
        return Result.success();
    }

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

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

    /**
     * 分享日记
     */
    @PostMapping(value = "/share")
    public Result<Void> share(@RequestParam String id) {
        boolean shared = diaryPostService.incrementShareCount(id);
        if (!shared) {
            return Result.error("分享失败");
        }
        return Result.success();
    }

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

    /**
     * 设置置顶优先级
     */
    @PutMapping(value = "/setPriority")
    public Result<Void> setPriority(@RequestParam String id, @RequestParam Integer priority) {
        boolean set = diaryPostService.setPriority(id, priority);
        if (!set) {
            return Result.error("设置优先级失败");
        }
        return Result.success();
    }
}