package com.smppw.modaq.application.api;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.smppw.modaq.application.service.EmailParseApiService;
import com.smppw.modaq.domain.dto.UploadReportParams;
import com.smppw.modaq.domain.dto.UploadReportResult;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/daq-parse")
public class ParseApi {
    private final EmailParseApiService service;

    public ParseApi(EmailParseApiService service) {
        this.service = service;
    }

    @GetMapping("health")
    public ResponseEntity<String> health() {
        return ResponseEntity.ok("ok");
    }

    @GetMapping("report")
    public ResponseEntity<String> report(String startDateTime) {
        Date now = new Date();
        Date preDate;
        if (StrUtil.isBlank(startDateTime)) {
            preDate = DateUtil.offsetDay(now, -1);
        } else {
            preDate = DateUtil.parseDateTime(startDateTime);
        }
        this.service.parseEmail(preDate, now, null, null);
        return ResponseEntity.ok("success");
    }

    /**
     * 批量解析（文件路径）
     *
     * @param params 参数
     * @return 解析结果
     */
    @PostMapping("upload-parse")
    public ResponseEntity<List<UploadReportResult>> uploadReport(@RequestBody UploadReportParams params) {
        return ResponseEntity.ok(this.service.uploadReport(params));
    }

    /**
     * 批量上传文件流并解析
     *
     * @param files 文件流对象
     * @return /
     */
    @PostMapping("upload-file-parse")
    public ResponseEntity<List<UploadReportResult>> batchUploadReport(@RequestParam("files") MultipartFile[] files) {
        return ResponseEntity.ok(this.service.batchUploadReport(files));
    }
}
