package com.tashow.cloud.infra.controller.admin.file;

import static com.tashow.cloud.common.pojo.CommonResult.success;

import com.tashow.cloud.common.pojo.CommonResult;
import com.tashow.cloud.common.pojo.PageResult;
import com.tashow.cloud.common.util.object.BeanUtils;
import com.tashow.cloud.infra.controller.admin.file.vo.config.FileConfigPageReqVO;
import com.tashow.cloud.infra.controller.admin.file.vo.config.FileConfigRespVO;
import com.tashow.cloud.infra.controller.admin.file.vo.config.FileConfigSaveReqVO;
import com.tashow.cloud.infra.dal.dataobject.file.FileConfigDO;
import com.tashow.cloud.infra.service.file.FileConfigService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/** 管理后台 - 文件配置 */
@RestController
@RequestMapping("/infra/file-config")
@Validated
public class FileConfigController {

  @Resource private FileConfigService fileConfigService;

  /** 创建文件配置 */
  @PostMapping("/create")
  @PreAuthorize("@ss.hasPermission('infra:file-config:create')")
  public CommonResult<Long> createFileConfig(@Valid @RequestBody FileConfigSaveReqVO createReqVO) {
    return success(fileConfigService.createFileConfig(createReqVO));
  }

  /** 更新文件配置 */
  @PutMapping("/update")
  @PreAuthorize("@ss.hasPermission('infra:file-config:update')")
  public CommonResult<Boolean> updateFileConfig(
      @Valid @RequestBody FileConfigSaveReqVO updateReqVO) {
    fileConfigService.updateFileConfig(updateReqVO);
    return success(true);
  }

  /** 更新文件配置为 Master */
  @PutMapping("/update-master")
  @PreAuthorize("@ss.hasPermission('infra:file-config:update')")
  public CommonResult<Boolean> updateFileConfigMaster(@RequestParam("id") Long id) {
    fileConfigService.updateFileConfigMaster(id);
    return success(true);
  }

  /** 删除文件配置 */
  @DeleteMapping("/delete")
  @PreAuthorize("@ss.hasPermission('infra:file-config:delete')")
  public CommonResult<Boolean> deleteFileConfig(@RequestParam("id") Long id) {
    fileConfigService.deleteFileConfig(id);
    return success(true);
  }

  /** 获得文件配置 */
  @GetMapping("/get")
  @PreAuthorize("@ss.hasPermission('infra:file-config:query')")
  public CommonResult<FileConfigRespVO> getFileConfig(@RequestParam("id") Long id) {
    FileConfigDO config = fileConfigService.getFileConfig(id);
    return success(BeanUtils.toBean(config, FileConfigRespVO.class));
  }

  /** 获得文件配置分页 */
  @GetMapping("/page")
  @PreAuthorize("@ss.hasPermission('infra:file-config:query')")
  public CommonResult<PageResult<FileConfigRespVO>> getFileConfigPage(
      @Valid FileConfigPageReqVO pageVO) {
    PageResult<FileConfigDO> pageResult = fileConfigService.getFileConfigPage(pageVO);
    return success(BeanUtils.toBean(pageResult, FileConfigRespVO.class));
  }

  /** 测试文件配置是否正确 */
  @GetMapping("/test")
  @PreAuthorize("@ss.hasPermission('infra:file-config:query')")
  public CommonResult<String> testFileConfig(@RequestParam("id") Long id) throws Exception {
    String url = fileConfigService.testFileConfig(id);
    return success(url);
  }
}
