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 org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

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

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

    @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");
    }

    @GetMapping("reparse")
    public ResponseEntity<String> reparseReport(Integer emailId) {
        this.service.reparseEmail(emailId);
        return ResponseEntity.ok("success");
    }
}
