package cn.iocoder.byzs.module.ai.controller.admin.video;

import cn.hutool.core.util.ObjUtil;
import cn.iocoder.byzs.framework.common.pojo.CommonResult;
import cn.iocoder.byzs.framework.common.pojo.PageResult;
import cn.iocoder.byzs.framework.common.util.object.BeanUtils;
import cn.iocoder.byzs.module.ai.controller.admin.video.vo.*;
import cn.iocoder.byzs.module.ai.dal.dataobject.video.AiVideoDO;
import cn.iocoder.byzs.module.ai.service.video.AiVideoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.iocoder.byzs.framework.common.pojo.CommonResult.success;
import static cn.iocoder.byzs.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "管理后台 - AI 视频")
@RestController
@RequestMapping("/ai/video")
@Slf4j
public class AiVideoController {

    @Resource
    private AiVideoService videoService;

    @GetMapping("/my-page")
    @Operation(summary = "获取【我的】视频分页")
    public CommonResult<PageResult<AiVideoRespVO>> getVideoPageMy(@Validated AiVideoPageReqVO pageReqVO) {
        PageResult<AiVideoDO> pageResult = videoService.getVideoPageMy(getLoginUserId(), pageReqVO);
        return success(BeanUtils.toBean(pageResult, AiVideoRespVO.class));
    }

    @GetMapping("/public-page")
    @Operation(summary = "获取公开的视频分页")
    public CommonResult<PageResult<AiVideoRespVO>> getVideoPagePublic(AiVideoPublicPageReqVO pageReqVO) {
        PageResult<AiVideoDO> pageResult = videoService.getVideoPagePublic(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AiVideoRespVO.class));
    }

    @GetMapping("/get-my")
    @Operation(summary = "获取【我的】视频记录")
    @Parameter(name = "id", required = true, description = "视频编号", example = "1024")
    public CommonResult<AiVideoRespVO> getVideoMy(@RequestParam("id") Long id) {
        AiVideoDO video = videoService.getVideo(id);
        if (video == null || ObjUtil.notEqual(getLoginUserId(), video.getUserId())) {
            return success(null);
        }
        return success(BeanUtils.toBean(video, AiVideoRespVO.class));
    }

    @GetMapping("/my-list-by-ids")
    @Operation(summary = "获取【我的】视频记录列表")
    @Parameter(name = "ids", required = true, description = "视频编号数组", example = "1024,2048")
    public CommonResult<List<AiVideoRespVO>> getVideoListMyByIds(@RequestParam("ids") List<Long> ids) {
        List<AiVideoDO> videoList = videoService.getVideoList(ids);
        videoList.removeIf(item -> !ObjUtil.equal(getLoginUserId(), item.getUserId()));
        return success(BeanUtils.toBean(videoList, AiVideoRespVO.class));
    }

    @Operation(summary = "生成视频")
    @PostMapping("/draw")
    public CommonResult<Long> drawVideo(@Valid @RequestBody AiVideoDrawReqVO drawReqVO) {
        return success(videoService.drawVideo(getLoginUserId(), drawReqVO));
    }

    @Operation(summary = "删除【我的】视频记录")
    @DeleteMapping("/delete-my")
    @Parameter(name = "id", required = true, description = "视频编号", example = "1024")
    public CommonResult<Boolean> deleteVideoMy(@RequestParam("id") Long id) {
        videoService.deleteVideoMy(id, getLoginUserId());
        return success(true);
    }

    // ================ 视频管理 ================

    @GetMapping("/page")
    @Operation(summary = "获得视频分页")
    @PreAuthorize("@ss.hasPermission('ai:video:query')")
    public CommonResult<PageResult<AiVideoRespVO>> getVideoPage(@Valid AiVideoPageReqVO pageReqVO) {
        PageResult<AiVideoDO> pageResult = videoService.getVideoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AiVideoRespVO.class));
    }

    @PutMapping("/update")
    @Operation(summary = "更新视频")
    @PreAuthorize("@ss.hasPermission('ai:video:update')")
    public CommonResult<Boolean> updateVideo(@Valid @RequestBody AiVideoUpdateReqVO updateReqVO) {
        videoService.updateVideo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除视频")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ai:video:delete')")
    public CommonResult<Boolean> deleteVideo(@RequestParam("id") Long id) {
        videoService.deleteVideo(id);
        return success(true);
    }

}