package com.agileboot.reader.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.agileboot.common.core.core.R;
import com.agileboot.common.mybatis.core.page.PageR;
import com.agileboot.reader.dto.TextFileQueryDTO;
import com.agileboot.reader.service.ITextFileService;
import com.agileboot.reader.vo.TextFileContentVO;
import com.agileboot.reader.vo.TextFileVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 文本文件管理Controller
 */
@Slf4j
@RestController
@RequestMapping("/reader/file")
@RequiredArgsConstructor
@Api(tags = "文本文件管理")
public class TextFileController {

    private final ITextFileService textFileService;

    @PostMapping("/upload")
    @ApiOperation("上传文本文件")
    public R<Long> uploadFile(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "description", required = false) String description) {
        Long fileId = textFileService.uploadFile(file, description);
        return R.ok(fileId);
    }

    @GetMapping("/list")
    @ApiOperation("获取文本文件列表（后台管理）")
    public PageR<TextFileVO> getFileList(TextFileQueryDTO query) {
        PageR<TextFileVO> page = textFileService.getFileList(query);
        return page;
    }

    @GetMapping("/detail/{fileId}")
    @ApiOperation("获取文件详情")
    public R<TextFileVO> getFileDetail(@PathVariable Long fileId) {
        TextFileVO file = textFileService.getFileDetail(fileId);
        return R.ok(file);
    }

    @DeleteMapping("/{fileId}")
    @ApiOperation("删除文件")
    public R<Void> deleteFile(@PathVariable Long fileId) {
        textFileService.deleteFile(fileId);
        return R.ok();
    }

    @DeleteMapping("/batch")
    @ApiOperation("批量删除文件")
    public R<Void> deleteBatch(@RequestBody List<Long> fileIds) {
        textFileService.deleteBatch(fileIds);
        return R.ok();
    }

    @PutMapping("/{fileId}/status/{status}")
    @ApiOperation("更新文件状态")
    public R<Void> updateStatus(@PathVariable Long fileId, @PathVariable Integer status) {
        textFileService.updateStatus(fileId, status);
        return R.ok();
    }
}

