package org.dromara.ai.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.ai.domain.bo.AiPaintingRecordBo;
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.Arrays;
import java.util.List;
import java.util.Map;

/**
 * AI绘画历史记录管理
 *
 * @author pengles
 */
@Tag(name = "管理端-AI绘画历史记录管理", description = "AI绘画历史记录管理接口")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ai/painting/record")
public class AdminAiPaintingRecordController extends BaseController {

    private final AiPaintingRecordService aiPaintingRecordService;

    /**
     * 查询AI绘画历史记录列表
     */
    @Operation(summary = "查询AI绘画历史记录列表", description = "分页查询AI绘画历史记录列表，支持多条件搜索")
    @SaCheckPermission("ai:painting:record:list")
    @GetMapping("/list")
    public TableDataInfo<AiPaintingRecordVo> list(@Parameter(description = "绘画历史记录查询条件") AiPaintingRecordBo bo,
                                                    @Parameter(description = "分页参数") PageQuery pageQuery) {
        return aiPaintingRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出AI绘画历史记录列表
     */
    @Operation(summary = "导出AI绘画历史记录列表", description = "导出AI绘画历史记录到Excel")
    @SaCheckPermission("ai:painting:record:export")
    @Log(title = "AI绘画历史记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Parameter(description = "绘画历史记录查询条件") AiPaintingRecordBo bo,
                       @Parameter(description = "响应对象") HttpServletResponse response) {
        List<AiPaintingRecordVo> list = aiPaintingRecordService.queryList(bo);
        ExcelUtil.exportExcel(list, "AI绘画历史记录", AiPaintingRecordVo.class, response);
    }

    /**
     * 获取AI绘画历史记录详细信息
     */
    @Operation(summary = "获取AI绘画历史记录详情", description = "根据记录ID获取详细信息")
    @SaCheckPermission("ai:painting:record:query")
    @GetMapping("/{id}")
    public R<AiPaintingRecordVo> getInfo(@Parameter(description = "记录ID", required = true)
                                          @NotNull(message = "主键不能为空")
                                          @PathVariable Long id) {
        return R.ok(aiPaintingRecordService.queryById(id));
    }

    /**
     * 根据任务ID获取记录
     */
    @Operation(summary = "根据任务ID获取记录", description = "根据任务ID获取绘画记录")
    @SaCheckPermission("ai:painting:record:query")
    @GetMapping("/task/{taskId}")
    public R<AiPaintingRecordVo> getByTaskId(@Parameter(description = "任务ID", required = true)
                                               @NotNull(message = "任务ID不能为空")
                                               @PathVariable String taskId) {
        return R.ok(aiPaintingRecordService.queryByTaskId(taskId));
    }

    /**
     * 批量删除AI绘画历史记录
     */
    @Operation(summary = "批量删除AI绘画历史记录", description = "根据ID数组批量删除AI绘画历史记录")
    @SaCheckPermission("ai:painting:record:remove")
    @Log(title = "AI绘画历史记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@Parameter(description = "记录ID数组", required = true)
                           @NotNull(message = "主键不能为空")
                           @PathVariable Long[] ids) {
        return toAjax(aiPaintingRecordService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 生成分享码
     */
    @Operation(summary = "生成分享码", description = "为指定的绘画作品生成分享码")
    @SaCheckPermission("ai:painting:record:edit")
    @Log(title = "AI绘画历史记录分享", businessType = BusinessType.UPDATE)
    @PostMapping("/{id}/share")
    public R<Map<String, String>> generateShareCode(@Parameter(description = "记录ID", required = true)
                                                     @NotNull(message = "记录ID不能为空")
                                                     @PathVariable Long id) {
        String shareCode = aiPaintingRecordService.generateShareCode(id);
        return R.ok(Map.of("shareCode", shareCode));
    }

    /**
     * 取消分享
     */
    @Operation(summary = "取消分享", description = "取消指定绘画作品的分享")
    @SaCheckPermission("ai:painting:record:edit")
    @Log(title = "AI绘画历史记录取消分享", businessType = BusinessType.UPDATE)
    @DeleteMapping("/{id}/share")
    public R<Void> cancelShare(@Parameter(description = "记录ID", required = true)
                                @NotNull(message = "记录ID不能为空")
                                @PathVariable Long id) {
        return toAjax(aiPaintingRecordService.cancelShare(id));
    }

}