package cn.reghao.tnb.file.app.zdisk.controller;

import cn.reghao.jutil.jdk.web.db.PageList;
import cn.reghao.jutil.jdk.web.result.Result;
import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.common.auth.AuthUser;
import cn.reghao.tnb.common.db.SelectOption;
import cn.reghao.tnb.file.app.zdisk.model.dto.AlbumEdit;
import cn.reghao.tnb.file.app.zdisk.model.dto.AlbumCreate;
import cn.reghao.tnb.file.app.zdisk.model.vo.DiskAlbumDetail;
import cn.reghao.tnb.file.app.zdisk.model.vo.DiskAlbumInfo;
import cn.reghao.tnb.file.app.zdisk.model.vo.DiskFileDetail;
import cn.reghao.tnb.file.app.zdisk.service.DiskAlbumService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author reghao
 * @date 2025-09-01 14:23:45
 */
@Tag(name = "网盘相册接口")
@RestController
@RequestMapping("/api/disk/album")
@AuthUser
public class DiskAlbumController {
    private final DiskAlbumService diskAlbumService;

    public DiskAlbumController(DiskAlbumService diskAlbumService) {
        this.diskAlbumService = diskAlbumService;
    }

    @Operation(summary = "创建相册", description = "N")
    @PostMapping(value = "/create", produces = MediaType.APPLICATION_JSON_VALUE)
    public String createDiskAlbum(@RequestBody @Validated AlbumCreate albumCreate) {
        diskAlbumService.createAlbum(albumCreate);
        return WebResult.success();
    }

    @Operation(summary = "编辑相册", description = "N")
    @PostMapping(value = "/edit", produces = MediaType.APPLICATION_JSON_VALUE)
    public String editDiskAlbum(@RequestBody @Validated AlbumEdit albumEdit) {
        int operation = albumEdit.getOperation();
        Result result = Result.fail("operation unknown");
        if (operation == 1) {
            result = diskAlbumService.addAlbumFile(albumEdit.getAlbumId(), albumEdit.getFileIds());
        } else if (operation == 2) {
            result = diskAlbumService.updateAlbumFilePosition(albumEdit.getAlbumId(), albumEdit.getFileIds());
        } else if (operation == 3) {
            result = diskAlbumService.deleteAlbumFile(albumEdit.getAlbumId(), albumEdit.getFileIds());
        }

        return WebResult.result(result);
    }

    @Operation(summary = "获取相册 KV 列表", description = "N")
    @GetMapping(value = "/kv", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getAlbumKeyValues() {
        List<SelectOption> keyValues = diskAlbumService.getAlbumKeyValues();
        return WebResult.success(keyValues);
    }

    @Operation(summary = "获取相册列表", description = "N")
    @GetMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getDiskAlbum() {
        List<DiskAlbumInfo> diskAlbumList = diskAlbumService.getAlbumList();
        return WebResult.success(diskAlbumList);
    }

    @Operation(summary = "获取相册详情", description = "N")
    @GetMapping(value = "/detail/{albumId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getDiskAlbumDetail(@PathVariable("albumId") long albumId, @RequestParam("pn") int pageNumber) {
        DiskAlbumDetail diskAlbumDetail = diskAlbumService.getDiskAlbumDetail(albumId, pageNumber);
        if (diskAlbumDetail != null) {
            return WebResult.success(diskAlbumDetail);
        }

        return WebResult.fail();
    }

    @Operation(summary = "获取合集中不存在的文件列表", description = "N")
    @GetMapping("/exclude")
    public String getAlbumExcludeFiles(@RequestParam("fileType") int fileType, @RequestParam("pn") int pageNumber) {
        PageList<DiskFileDetail> pageList = diskAlbumService.getAlbumExcludeFiles(fileType, pageNumber);
        return WebResult.success(pageList);
    }
}
