package cn.reghao.tnb.content.app.vod.controller;

import cn.reghao.file.api.dto.JobInfo;
import cn.reghao.jutil.jdk.web.db.PageList;
import cn.reghao.tnb.common.web.ServletUtil;
import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.auth.api.iface.AdminAccountService;
import cn.reghao.tnb.content.api.dto.UserVideoPost;
import cn.reghao.tnb.content.api.dto.VideoRegion;
import cn.reghao.tnb.content.api.dto.VideoSearch;
import cn.reghao.tnb.content.api.dto.VideoUrls;
import cn.reghao.tnb.common.db.SelectOption;
import cn.reghao.tnb.content.app.vod.model.dto.CategoryAdd;
import cn.reghao.tnb.content.app.vod.model.dto.NoticeAdd;
import cn.reghao.tnb.content.app.vod.service.AdminVodService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2025-09-26 20:03:57
 */
@Tag(name = "vod 管理接口")
@RestController
@RequestMapping("/api/admin/content/vod")
public class AdminVodController {
    @DubboReference(check = false, timeout = 60_000)
    private AdminAccountService adminAccountService;
    private final AdminVodService adminVodService;

    public AdminVodController(AdminVodService adminVodService) {
        this.adminVodService = adminVodService;
    }

    @Operation(summary = "稿件列表", description = "N")
    @GetMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public String postIndex(VideoSearch videoSearch
                            /*@RequestParam(value = "durationGt", required = false) String durationGt,
                            @RequestParam(value = "direction", required = false) String direction,
                            @RequestParam(value = "orderBy", required = false) String orderBy,*/) {
        int pageNumber = Integer.parseInt(ServletUtil.getRequestParam("pageNo", "1"));
        videoSearch.setPageNo(pageNumber);
        videoSearch.setOrderBy("publishAt");
        videoSearch.setOrderDirection("DESC");

        PageList<UserVideoPost> pageList = adminVodService.getUserVideoPosts(videoSearch);

        String durationGt = "";
        List<SelectOption> durationList = List.of(new SelectOption("2", "2"), new SelectOption("5", "5"), new SelectOption("10", "10"));

        String direction = "";
        List<SelectOption> directionList = List.of(new SelectOption("v", "v"), new SelectOption("h", "h"));

        String orderBy = "";
        List<SelectOption> orderByList = List.of(new SelectOption("duration", "duration"), new SelectOption("publishAt", "publishAt"));
        return WebResult.success(pageList);
    }

    @Operation(summary = "稿件预览", description = "N")
    @GetMapping(value = "/video_preview/{videoId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String previewPage(@PathVariable(value = "videoId") String videoId) {
        VideoUrls videoInfo = adminVodService.getVideoUrl(videoId);
        return WebResult.success(videoInfo);
    }

    @Operation(summary = "延时任务列表", description = "N")
    @GetMapping(value = "/video_convert", produces = MediaType.APPLICATION_JSON_VALUE)
    public String videoConvertList() {
        PageList<JobInfo> pageList = adminVodService.getJobs();
        return WebResult.success(pageList);
    }

    @Operation(summary = "站点公告", description = "N")
    @GetMapping(value = "/site_notice", produces = MediaType.APPLICATION_JSON_VALUE)
    public String siteNoticePage() {
        String content = adminVodService.getSiteNotice();
        return WebResult.success(content);
    }

    @Operation(summary = "添加站点公告", description = "N")
    @PostMapping(value = "/site_notice", produces = MediaType.APPLICATION_JSON_VALUE)
    public String addSiteNotice(@RequestBody @Validated NoticeAdd noticeAdd) {
        adminVodService.setSiteNotice(noticeAdd);
        return WebResult.success();
    }

    @Operation(summary = "视频分区", description = "N")
    @GetMapping(value = "/video_category", produces = MediaType.APPLICATION_JSON_VALUE)
    public String index() {
        List<VideoRegion> list = adminVodService.getVideoRegions();
        List<VideoRegion> regions = list.stream()
                .map(VideoRegion::getChildren)
                .flatMap(Collection::stream)
                .collect(Collectors.toList());
        return WebResult.success(regions);
    }

    @Operation(summary = "添加视频分区", description = "N")
    @PostMapping(value = "/video_category", produces = MediaType.APPLICATION_JSON_VALUE)
    public String addVideoCategory(@RequestBody @Validated CategoryAdd categoryAdd) {
        adminVodService.addVideoCategory(categoryAdd);
        return WebResult.success();
    }
}
