package com.infoclinika.pdx.controller;

import com.infoclinika.pdx.service.FileParser;
import com.infoclinika.pdx.service.ParsedFile;
import com.infoclinika.pdx.service.protein.ProteinService;
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 requests to upload Protein data.
 */
@RestController
@RequestMapping("/api/upload")
public class ProteinUploadController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private static final String FILE_PARAM = "file";

    private final FileParser fileParser;
    private final ProteinService proteinService;

    /**
     * Constructor to provide required services.
     */
    @Autowired
    public ProteinUploadController(FileParser fileParser, ProteinService proteinService) {
        this.fileParser = fileParser;
        this.proteinService = proteinService;
    }

    /**
     * Handles upload of protein domain data.
     *
     * @param file file to parse
     */
    @PostMapping("/protein")
    @ResponseStatus(HttpStatus.OK)
    public void uploadProteinDomains(@RequestParam(FILE_PARAM) MultipartFile file) {
        logger.debug("Request to /api/upload/protein");

        final ParsedFile parsedFile = fileParser.parseFile(file);
        proteinService.save(parsedFile);
    }
}
