package com.luojia_channel.modules.post.controller;


import com.luojia_channel.common.domain.Result;
import com.luojia_channel.common.domain.page.PageResponse;
import com.luojia_channel.common.domain.page.ScrollPageResponse;
import com.luojia_channel.modules.post.dto.req.PostSaveDTO;
import com.luojia_channel.modules.post.dto.req.PostPageQueryDTO;
import com.luojia_channel.modules.post.dto.resp.PostBasicInfoDTO;
import com.luojia_channel.modules.post.dto.resp.PostInfoDTO;
import com.luojia_channel.modules.post.service.PostService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RequiredArgsConstructor
@RestController
@RequestMapping("/post")
@Tag(name = "帖子模块", description = "帖子相关接口")
public class PostController {

    private final PostService postService;

    // 创建帖子
    @PostMapping
    @Operation(
            summary = "创建帖子",
            tags = {"帖子模块"}
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "创建成功"),
            @ApiResponse(responseCode = "500", description = "创建失败，请稍后重试")
    })
    public Result<Long> savePost(@RequestBody PostSaveDTO postSaveDTO) {
        return Result.success(postService.savePost(postSaveDTO));
    }

    // 设置帖子封面
    @PostMapping("/cover")
    @Operation(
            summary = "设置帖子封面",
            tags = {"帖子模块"}
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "设置成功"),
            @ApiResponse(responseCode = "500", description = "设置失败，请稍后重试")
    })
    public Result<String> setCover(@RequestParam("file") MultipartFile file){
        return Result.success(postService.setCover(file));
    }

    // 更新帖子
    @PutMapping
    @Operation(
            summary = "更新帖子",
            tags = {"帖子模块"}
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "更新成功"),
            @ApiResponse(responseCode = "500", description = "更新失败，请稍后重试")
    })
    public Result<Void> updatePost(@RequestBody PostSaveDTO postSaveDTO) {
        postService.updatePost(postSaveDTO);
        return Result.success();
    }

    // 删除帖子
    @DeleteMapping
    @Operation(
            summary = "删除帖子",
            tags = {"帖子模块"}
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "删除成功"),
            @ApiResponse(responseCode = "500", description = "删除失败，请稍后重试")
    })
    public Result<Void> deletePost(@RequestParam("id") Long id) {
        postService.deletePost(id);
        return Result.success();
    }

    // 根据ID获取帖子详情
    @GetMapping("/detail")
    @Operation(
            summary = "根据ID获取帖子详情",
            tags = {"帖子模块"}
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "获取失败，帖子不存在或被删除")
    })
    public Result<PostInfoDTO> getPostDetail(@RequestParam("id") Long id) {
        return Result.success(postService.getPostDetail(id));
    }

    // 分页查询帖子
    @GetMapping("/list")
    @Operation(
            summary = "分页查询帖子",
            tags = {"帖子模块"}
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "获取失败，请稍后重试")
    })
    public Result<ScrollPageResponse<PostBasicInfoDTO>> pagePost(PostPageQueryDTO postPageQueryDTO) {
        return Result.success(postService.pagePost(postPageQueryDTO));
    }

    // 查看用户的帖子
    @GetMapping("/user")
    @Operation(
            summary = "查看用户的帖子",
            tags = {"帖子模块"}
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "获取失败，请稍后重试")
    })
    public Result<ScrollPageResponse<PostBasicInfoDTO>> pagePostOfUser(PostPageQueryDTO postPageQueryDTO) {
        return Result.success(postService.pagePostOfUser(postPageQueryDTO));
    }

    // 点赞帖子
    @PutMapping("/like/{id}")
    @Operation(
            summary = "点赞帖子",
            tags = {"帖子模块"}
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "点赞成功"),
            @ApiResponse(responseCode = "500", description = "点赞失败，帖子不存在或被删除")
    })
    public Result<Void> likePost(@PathVariable("id") Long id) {
        postService.likePost(id);
        return Result.success();
    }
}
