package es.uv.garcosda.data.controllers;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import es.uv.garcosda.data.repository.MailRepository;
import es.uv.garcosda.data.domain.Mail;

@RestController
@RequestMapping("/api/v1/")
public class DataController {

	@Autowired
	private MailRepository mr;
	
	@GetMapping
	public ResponseEntity<?> getMails(@RequestParam(value="from", required = false) Optional<String> from, 
			@RequestParam(value="to", required = false) Optional<String> to) {
		
		List<Mail> mails;
		
		if(from.isPresent()) {
			mails = mr.findByFrom(from.get());
		}
		else if(to.isPresent()){
			mails = mr.findByTo(to.get());
		}
		else {
			mails = (List<Mail>) mr.findAll();
		}
		
		return new ResponseEntity<List<Mail>>(mails, HttpStatus.OK);
	}
	
	
}
