package org.dromara.ai.controller.app;

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 jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.ai.domain.bo.AiPaintingFavoriteBo;
import org.dromara.ai.domain.vo.AiPaintingFavoriteVo;
import org.dromara.ai.service.AiPaintingFavoriteService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户端AI绘画作品收藏
 *
 * @author pengles
 */
@Tag(name = "用户端-AI绘画作品收藏", description = "用户端AI绘画作品收藏接口")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/ai/painting/favorite")
public class AppAiPaintingFavoriteController extends BaseController {

    private final AiPaintingFavoriteService aiPaintingFavoriteService;

    /**
     * 查询当前用户的收藏列表
     */
    @Operation(summary = "查询当前用户的收藏列表", description = "查询当前登录用户的绘画作品收藏列表")
    @GetMapping("/list")
    public R<List<AiPaintingFavoriteVo>> getMyFavorites() {
        Long userId = LoginHelper.getUserId();
        List<AiPaintingFavoriteVo> list = aiPaintingFavoriteService.queryByUserId(userId);
        return R.ok(list);
    }

    /**
     * 收藏绘画作品
     */
    @Operation(summary = "收藏绘画作品", description = "收藏指定的绘画作品")
    @PostMapping
    public R<Void> addFavorite(@Valid @RequestBody AiPaintingFavoriteBo bo) {
        // 自动设置用户ID
        Long userId = LoginHelper.getUserId();
        bo.setUserId(userId);
        return toAjax(aiPaintingFavoriteService.insertByBo(bo));
    }

    /**
     * 取消收藏
     */
    @Operation(summary = "取消收藏", description = "取消收藏指定的绘画作品")
    @DeleteMapping("/{paintingRecordId}")
    public R<Void> cancelFavorite(@Parameter(description = "绘画记录ID", required = true)
                                   @NotNull(message = "绘画记录ID不能为空")
                                   @PathVariable Long paintingRecordId) {
        Long userId = LoginHelper.getUserId();
        return toAjax(aiPaintingFavoriteService.cancelFavorite(userId, paintingRecordId));
    }

    /**
     * 检查是否已收藏
     */
    @Operation(summary = "检查是否已收藏", description = "检查当前用户是否已收藏指定的绘画作品")
    @GetMapping("/check/{paintingRecordId}")
    public R<Boolean> checkFavorite(@Parameter(description = "绘画记录ID", required = true)
                                     @NotNull(message = "绘画记录ID不能为空")
                                     @PathVariable Long paintingRecordId) {
        Long userId = LoginHelper.getUserId();
        Boolean favorited = aiPaintingFavoriteService.isFavorited(userId, paintingRecordId);
        return R.ok(favorited);
    }

}
