package com.example.backend.controller;

import com.example.backend.common.ApiResponse;
import com.example.backend.dto.CommitFileResult;
import com.example.backend.dto.DingtalkUserInfo;
import com.example.backend.service.DingtalkService;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/api")
@Validated
public class DingtalkController {
  private final DingtalkService dingtalkService;

  public DingtalkController(DingtalkService dingtalkService) {
    this.dingtalkService = dingtalkService;
  }

  /**
   * 获取用户信息
   *
   * @param code
   * @param corpId
   * @return
   */
  @GetMapping("/getUserInfo")
  public ApiResponse<DingtalkUserInfo> getUserInfo(
    @RequestParam @NotBlank String code,
    @RequestParam @NotBlank String corpId) {
    DingtalkUserInfo userInfo = dingtalkService.getUserInfo(code, corpId);
    return ApiResponse.ok(userInfo);
  }

  /**
   * 文件上传功能
   *
   * @param file
   * @param unionId
   * @param dentryName
   * @param size
   * @return
   */
  @PostMapping("/commit-file")
  public ApiResponse<CommitFileResult> commitFile(
    @RequestParam("file") MultipartFile file,
    @RequestParam @NotBlank String unionId,
    @RequestParam @NotBlank String dentryName,
    @RequestParam @NotNull Long size) {
    CommitFileResult result = dingtalkService.commitFile(file, unionId, dentryName, size);
    return ApiResponse.ok(result);
  }

  @GetMapping("/downfile")
  public ApiResponse<Object> DownFileLoad(@RequestParam String unionId) {
    String result = dingtalkService.DownLoadFile(unionId);
    System.out.println(result);
    if (result != null && result.equals("ok")) {
      return ApiResponse.ok("ok");
    }else {
      return ApiResponse.ok("false");
    }
  }
} 