package com.xindazhou.business.controller.admin.community.post;

import com.xindazhou.business.controller.admin.community.post.vo.PostPageReqVO;
import com.xindazhou.business.controller.admin.community.post.vo.PostRespVO;
import com.xindazhou.business.controller.admin.community.post.vo.PostUpdateStatusReqVO;
import com.xindazhou.business.service.community.post.PostService;
import com.xindazhou.framework.common.pojo.CommonResult;
import com.xindazhou.framework.common.pojo.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.xindazhou.framework.common.pojo.CommonResult.success;

/**
 * 管理后台 - 帖子 Controller
 *
 * @author xindazhou
 */
@Tag(name = "管理后台 - 帖子管理")
@RestController("communityPostController")
@RequestMapping("/admin-api/community/post")
@Validated
@Slf4j
public class PostController {

    @Resource
    private PostService postService;

    @GetMapping("/page")
    @Operation(summary = "获得帖子分页列表")
    @PreAuthorize("@ss.hasPermission('community:post:query')")
    public CommonResult<PageResult<PostRespVO>> getPostPage(@Valid PostPageReqVO pageReqVO) {
        PageResult<PostRespVO> pageResult = postService.getPostPageAdmin(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/get")
    @Operation(summary = "获得帖子")
    @Parameter(name = "id", description = "帖子ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('community:post:query')")
    public CommonResult<PostRespVO> getPost(@RequestParam("id") Long id) {
        // 使用移动端的getPost方法，然后转换为管理后台VO
        com.xindazhou.business.controller.app.community.post.vo.PostRespVO appPost = postService.getPost(id);
        PostRespVO adminPost = convertToAdminVO(appPost);
        return success(adminPost);
    }

    @PutMapping("/update-status")
    @Operation(summary = "更新帖子状态")
    @PreAuthorize("@ss.hasPermission('community:post:update')")
    public CommonResult<Boolean> updatePostStatus(@Valid @RequestBody PostUpdateStatusReqVO updateReqVO) {
        postService.updatePostStatus(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除帖子")
    @Parameter(name = "id", description = "帖子ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('community:post:delete')")
    public CommonResult<Boolean> deletePost(@RequestParam("id") Long id) {
        postService.deletePostAdmin(id);
        return success(true);
    }

    /**
     * 转换为管理后台VO
     */
    private PostRespVO convertToAdminVO(com.xindazhou.business.controller.app.community.post.vo.PostRespVO appPost) {
        PostRespVO adminPost = new PostRespVO();
        adminPost.setId(appPost.getId());
        adminPost.setUserId(appPost.getUserId());
        adminPost.setUserNickname(appPost.getUserNickname());
        adminPost.setTitle(appPost.getTitle());
        adminPost.setContent(appPost.getContent());
        adminPost.setContentType(appPost.getContentType());
        adminPost.setViewCount(appPost.getViewCount());
        adminPost.setLikeCount(appPost.getLikeCount());
        adminPost.setCommentCount(appPost.getCommentCount());
        adminPost.setCollectCount(appPost.getCollectCount());
        adminPost.setStatus(appPost.getStatus());
        adminPost.setIsTop(appPost.getIsTop());
        adminPost.setIsEssence(appPost.getIsEssence());
        adminPost.setTags(appPost.getTags());
        adminPost.setCreateTime(appPost.getCreateTime());
        
        // 转换媒体列表
        if (appPost.getMediaList() != null && !appPost.getMediaList().isEmpty()) {
            List<PostRespVO.PostMediaVO> mediaList = appPost.getMediaList().stream()
                    .map(appMedia -> {
                        PostRespVO.PostMediaVO adminMedia = new PostRespVO.PostMediaVO();
                        adminMedia.setMediaType(appMedia.getMediaType());
                        adminMedia.setMediaUrl(appMedia.getMediaUrl());
                        adminMedia.setCoverUrl(appMedia.getCoverUrl());
                        adminMedia.setDuration(appMedia.getDuration());
                        adminMedia.setFileSize(appMedia.getFileSize());
                        // sort字段在app端可能不存在，设置为0
                        adminMedia.setSort(0);
                        return adminMedia;
                    })
                    .collect(java.util.stream.Collectors.toList());
            adminPost.setMediaList(mediaList);
        }
        
        return adminPost;
    }

}

