package es.uv.saic.web;

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

import es.uv.saic.dto.CategoriaDTO;
import es.uv.saic.dto.DocumentTmpDTO;
import es.uv.saic.feign.DocumentClient;
import es.uv.saic.domain.Document;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;


@RestController
public class DocumentController {

    @Autowired
	private DocumentClient dc;

    /*
     * endpoint para save
     */
    @PostMapping("/document/save")
    public ResponseEntity<?> save(@RequestBody Document doc) {
        try {
            return ResponseEntity.ok(dc.save(doc));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.badRequest().body("Error comunicando con el servicio de documentos: " + e.getMessage());
        }
    }

    /*
     * endpoing para findByCategoriaOrgan
     */
    @PostMapping("/findByCategoriaOrgan")
    public Document findByCategoriaOrgan(@RequestBody CategoriaDTO categoria) {
        return dc.findByCategoriaOrgan(categoria);
    }

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

    /*
     * endpoint para upload
     */
    @PostMapping("/document/upload")
    public String upload(@RequestBody DocumentTmpDTO documentTmpDTO) {
        return dc.upload(documentTmpDTO);
    }

    @PostMapping("/document/archive")
    public ResponseEntity<?> archive(@RequestBody Document doc) {
        try {
            return ResponseEntity.ok(dc.archive(doc));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.badRequest().body("Error comunicando con servicio de documentos: " + e.getMessage());
        }
    }
}