package cn.reghao.tnb.content.app.data.controller;

import cn.reghao.jutil.jdk.web.db.PageList;
import cn.reghao.jutil.jdk.web.result.Result;
import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.common.auth.AuthUser;
import cn.reghao.tnb.common.auth.UserContext;
import cn.reghao.tnb.content.api.dto.ImagePublishSbt;
import cn.reghao.tnb.content.app.data.model.dto.AlbumImageData;
import cn.reghao.tnb.content.app.data.model.vo.AlbumCard;
import cn.reghao.tnb.content.app.data.service.ImagePostService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author reghao
 * @date 2023-10-07 09:55:50
 */
@Tag(name = "图片稿件接口")
@RestController
@RequestMapping("/api/content/post/image")
public class ImagePostController {
    private final ImagePostService imagePostService;

    public ImagePostController(ImagePostService imagePostService) {
        this.imagePostService = imagePostService;
    }

    @AuthUser
    @Operation(summary = "创建图片稿件", description = "N")
    @PostMapping(value = "/create", produces = MediaType.APPLICATION_JSON_VALUE)
    public String createImagePost(@RequestBody @Validated ImagePublishSbt imagePublishSbt) {
        Result result = imagePostService.create(imagePublishSbt);
        return WebResult.result(result);
    }

    @AuthUser
    @Operation(summary = "添加图片到图片稿件", description = "N")
    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
    public String addImage(@RequestBody @Validated AlbumImageData albumImageData) {
        Result result = imagePostService.add(albumImageData);
        return WebResult.result(result);
    }

    @AuthUser
    @Operation(summary = "获取用户发布的图片稿件列表", description = "N")
    @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getUserImageAlbum(@RequestParam("page") int page) {
        long userId = UserContext.getUserId();
        PageList<AlbumCard> pageList = imagePostService.getUserAlbums(userId, page);
        return WebResult.success(pageList);
    }

    @AuthUser
    @Operation(summary = "获取已发布图片稿件中的图片", description = "N")
    @GetMapping(value = "/{albumId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getImageAlbum(@PathVariable("albumId") long albumId, @RequestParam("page") int page) {
        String result = imagePostService.getImageAlbum(albumId, page);
        return result;
    }
}
