package es.uv.garcosda.data.controllers;

import java.util.Date;
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.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

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

	@Autowired
	private MailRepository mr;
	
	@Autowired
	private UserRepository ur;
	
	@GetMapping("status")
	public String status(){
		return "OK";
	}
	
	@GetMapping("mails")
	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);
	}
	
	@GetMapping("users")
	public ResponseEntity<?> getUsers() {
		List<User> u = (List<User>) ur.findAll();
		return new ResponseEntity<List<User>>(u, HttpStatus.OK);
	}
	
	@GetMapping("users/email/{email}")
	public ResponseEntity<?> getUserByEmail(@PathVariable String email) {
		User u = ur.findByEmail(email);
		if(u != null) {
			return new ResponseEntity<User>(u, HttpStatus.OK); 
		}
		return new ResponseEntity<User>(new User(), HttpStatus.NOT_FOUND);
	}
	
	@GetMapping("users/id/{id}")
	public ResponseEntity<?> getUserById(@PathVariable String id) {
		Optional<User> u = ur.findById(id);
		if(u.isPresent()) {
			return new ResponseEntity<User>(u.get(), HttpStatus.OK); 
		}
		return new ResponseEntity<User>(new User(), HttpStatus.NOT_FOUND);
	}
	
	@PostMapping("mails")
	public ResponseEntity<?> createMail(@RequestBody Mail mail) {
		mail.setDate(new Date(System.currentTimeMillis()));
		Mail m = mr.save(mail);
		if(m.getId() != null) {
			return new ResponseEntity<Mail>(m, HttpStatus.OK); 
		}
		return new ResponseEntity<Mail>(new Mail(), HttpStatus.BAD_REQUEST);
	}
	
	@PostMapping("users")
	public ResponseEntity<?> createUser(@RequestBody User user) {
		User u = ur.save(user);
		if(u.getId() != null) {
			return new ResponseEntity<User>(u, HttpStatus.OK); 
		}
		return new ResponseEntity<User>(new User(), HttpStatus.BAD_REQUEST);
	}
	
	@DeleteMapping("mails/{id}")
	public ResponseEntity<?> deleteEmail(@PathVariable String id) {
		Optional<Mail> m = mr.findById(id);
		if(!m.isPresent()) {
			return new ResponseEntity<String>("ID not found", HttpStatus.NOT_FOUND);
		}
		mr.delete(m.get());
		return new ResponseEntity<String>("", HttpStatus.OK);
	}
	
	@DeleteMapping("users/{id}")
	public ResponseEntity<?> deleteUser(@PathVariable String id) {
		Optional<User> u = ur.findById(id);
		if(!u.isPresent()) {
			return new ResponseEntity<String>("ID not found", HttpStatus.NOT_FOUND);
		}
		ur.delete(u.get());
		return new ResponseEntity<String>("", HttpStatus.OK);
	}
	
}
