package com.infoclinika.pdx.controller;

import com.infoclinika.pdx.service.FileParser;
import com.infoclinika.pdx.service.GeneService;
import com.infoclinika.pdx.service.ParsedFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * Handles file upload requests.
 */
@SuppressWarnings("DuplicateStringLiteralInspection")
@RestController
@RequestMapping("/api/upload")
public class FileUploadController {
    private static final String FILE_PARAM = "file";
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final FileParser fileParser;
    private final GeneService geneService;

    /**
     * Constructor to provide required services.
     */
    @Autowired
    public FileUploadController(FileParser fileParser, GeneService geneService) {
        this.fileParser = fileParser;
        this.geneService = geneService;
    }

    /**
     * Handles gene info table upload.
     *
     * @param file file to parse
     */
    @PostMapping("/gene-info")
    @ResponseStatus(HttpStatus.OK)
    public void postFiles(@RequestParam(FILE_PARAM) MultipartFile file) {
        logger.debug("Request to /gene-info");
        final ParsedFile parsedFile = fileParser.parseFile(file);
        geneService.saveGeneInfo(parsedFile);
    }
}
