package es.uv.saic.web;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import es.uv.saic.dto.ArchiveOrganDTO;
import es.uv.saic.dto.CategoriaDTO;
import es.uv.saic.dto.DocumentTmpDTO;
import es.uv.saic.domain.Document;
import es.uv.saic.service.DocumentService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@RestController
public class DocumentController {
    @Autowired
    private DocumentService ds;
    /*
     * endpoint para save
     */
    @PostMapping("/document/save")
    public Document save(@RequestBody Document doc) {
        return ds.save(doc); 
    }

    /*
     * endpoing para findByCategoriaOrgan
     */
    @PostMapping("/findByCategoriaOrgan")
    public Document findByCategoriaOrgan(@RequestBody CategoriaDTO categoria) {
        return ds.findByCategoriaOrgan(categoria.getCategoria(), categoria.getLugar(), categoria.getTlugar());
    }

    /*
     * endpoint para findByID
     */
    @GetMapping("/document/{idDocument}")
    public Document findByID(@PathVariable Integer idDocument) {
       return ds.findById(idDocument);
    }
    

    /*
     * endpoint para upload
     */
    @PostMapping("/document/upload")
    public String upload(@RequestBody DocumentTmpDTO documentTmpDTO) throws IOException {
        return ds.upload(documentTmpDTO.getIdDocument(), documentTmpDTO.getIdCategoria(),
            documentTmpDTO.getLugar(), documentTmpDTO.getTlugar(), documentTmpDTO.getFile());
    }

    @PostMapping("/document/archive")
    public Document archive(@RequestBody Document doc) {
        return ds.archive(doc);
    }

    @PostMapping("/archiveByOrgan")
    public void archiveByOrgan(@RequestBody ArchiveOrganDTO organ) {
        ds.archiveByOrgan(organ.getLugar(), organ.getTlugar());
    }
}