package net.educoder.ecsonar.controller;

import net.educoder.ecsonar.model.api.SonarRequest;
import net.educoder.ecsonar.services.EcsonarService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/api")
public class MainController {
    private Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    EcsonarService ecsonarService;

    @RequestMapping(value = "/sonar", method = RequestMethod.POST)
    @CrossOrigin
    public String sonar(@RequestBody SonarRequest sonarRequest) throws IOException {
        log.info("sonar: {}", sonarRequest);
        ecsonarService.sonar(sonarRequest);
        return "success";
    }

    @RequestMapping(value = "/sonarSingle", method = RequestMethod.POST)
    @CrossOrigin
    public String sonarSingle(@RequestBody SonarRequest sonarRequest) throws IOException {
        log.info("sonarSingle: {}", sonarRequest);
        ecsonarService.sonarSingle(sonarRequest);
        return "success";
    }

    @RequestMapping(value = "/fit", method = RequestMethod.GET)
    public String fit(@RequestParam String fileName,
                      @RequestParam String homeworkId) throws Exception {
        ecsonarService.fit(fileName, homeworkId);
        return "success";
    }


    @RequestMapping(value = "/scanExcel", method = RequestMethod.POST)
    public String scanExcel(@RequestParam String excelPath) throws Exception {
        ecsonarService.scanExcel(excelPath);
        return "success";
    }


}
