package org.dromara.ai.controller.app;

import cn.dev33.satoken.annotation.SaIgnore;
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.constraints.NotBlank;
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.vo.AiPaintingRecordVo;
import org.dromara.ai.service.AiPaintingRecordService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 用户端AI绘画记录
 *
 * @author pengles
 */
@Tag(name = "用户端-AI绘画记录", description = "用户端AI绘画记录接口")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/ai/painting/record")
public class AppAiPaintingRecordController extends BaseController {

    private final AiPaintingRecordService aiPaintingRecordService;

    /**
     * 生成分享码
     */
    @Operation(summary = "生成分享码", description = "为指定的绘画作品生成分享码")
    @PostMapping("/{id}/share")
    public R<Map<String, String>> generateShareCode(@Parameter(description = "记录ID", required = true)
                                                     @NotNull(message = "记录ID不能为空")
                                                     @PathVariable Long id) {
        // 验证记录所有权
        AiPaintingRecordVo record = aiPaintingRecordService.queryById(id);
        if (record == null) {
            return R.fail("绘画记录不存在");
        }
        Long userId = LoginHelper.getUserId();
        if (!userId.equals(record.getUserId())) {
            return R.fail("无权操作该记录");
        }
        String shareCode = aiPaintingRecordService.generateShareCode(id);
        return R.ok(Map.of("shareCode", shareCode));
    }

    /**
     * 取消分享
     */
    @Operation(summary = "取消分享", description = "取消指定绘画作品的分享")
    @DeleteMapping("/{id}/share")
    public R<Void> cancelShare(@Parameter(description = "记录ID", required = true)
                                @NotNull(message = "记录ID不能为空")
                                @PathVariable Long id) {
        // 验证记录所有权
        AiPaintingRecordVo record = aiPaintingRecordService.queryById(id);
        if (record == null) {
            return R.fail("绘画记录不存在");
        }
        Long userId = LoginHelper.getUserId();
        if (!userId.equals(record.getUserId())) {
            return R.fail("无权操作该记录");
        }
        return toAjax(aiPaintingRecordService.cancelShare(id));
    }

    /**
     * 根据分享码查看作品（无需登录）
     */
    @Operation(summary = "根据分享码查看作品", description = "根据分享码查看公开分享的绘画作品，无需登录")
    @SaIgnore
    @GetMapping("/share/{shareCode}")
    public R<AiPaintingRecordVo> getByShareCode(@Parameter(description = "分享码", required = true)
                                                  @NotBlank(message = "分享码不能为空")
                                                  @PathVariable String shareCode) {
        AiPaintingRecordVo record = aiPaintingRecordService.queryByShareCode(shareCode);
        if (record == null) {
            return R.fail("分享码无效或作品已取消分享");
        }
        return R.ok(record);
    }

}
