package cn.meowrain.aioj.backend.blogservice.controller;

import cn.meowrain.aioj.backend.framework.core.web.Result;
import cn.meowrain.aioj.backend.framework.core.web.Results;
import cn.meowrain.aioj.backend.blogservice.dto.req.DraftSaveRequestDTO;
import cn.meowrain.aioj.backend.blogservice.dto.resp.DraftResponseDTO;
import cn.meowrain.aioj.backend.blogservice.service.DraftService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 草稿箱控制器
 *
 * @author AIOJ
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/v1/draft")
@Tag(name = "草稿箱管理", description = "草稿保存、查询、删除等接口")
public class DraftController {

    private final DraftService draftService;

    @PostMapping("/save")
    @Operation(summary = "保存草稿", description = "保存或更新文章草稿")
    public Result<Long> saveDraft(
            @Parameter(description = "草稿信息", required = true)
            @Valid @RequestBody DraftSaveRequestDTO request) {
        Long draftId = draftService.saveDraft(request);
        return Results.success(draftId);
    }

    @DeleteMapping("/delete/{draftId}")
    @Operation(summary = "删除草稿", description = "删除指定草稿")
    public Result<Void> deleteDraft(
            @Parameter(description = "草稿ID", required = true)
            @PathVariable("draftId") Long draftId) {
        draftService.deleteDraft(draftId);
        return Results.success();
    }

    @GetMapping("/{draftId}")
    @Operation(summary = "查询草稿详情", description = "根据草稿ID查询草稿详细信息")
    public Result<DraftResponseDTO> getDraftById(
            @Parameter(description = "草稿ID", required = true)
            @PathVariable("draftId") Long draftId) {
        DraftResponseDTO draft = draftService.getDraftById(draftId);
        return Results.success(draft);
    }

    @GetMapping("/list")
    @Operation(summary = "分页查询草稿列表", description = "获取当前用户的草稿列表")
    public Result<IPage<DraftResponseDTO>> getDraftList(
            @Parameter(description = "页码", required = true)
            @RequestParam("current") Integer current,
            @Parameter(description = "每页数量", required = true)
            @RequestParam("size") Integer size) {
        IPage<DraftResponseDTO> page = draftService.getDraftList(current, size);
        return Results.success(page);
    }

    @GetMapping("/latest")
    @Operation(summary = "查询最新草稿", description = "获取最新的自动保存草稿")
    public Result<DraftResponseDTO> getLatestDraft() {
        DraftResponseDTO draft = draftService.getLatestDraft();
        return Results.success(draft);
    }
}
