package dev.peerat.backend.routes.users;

import java.util.regex.Matcher;

import dev.peerat.backend.bonus.extract.RouteDoc;
import dev.peerat.backend.model.PeerAtUser;
import dev.peerat.backend.repository.DatabaseRepository;
import dev.peerat.framework.Context;
import dev.peerat.framework.HttpReader;
import dev.peerat.framework.HttpWriter;
import dev.peerat.framework.RequestType;
import dev.peerat.framework.Response;
import dev.peerat.framework.Route;
import dev.peerat.framework.utils.json.JsonMap;

public class ChangePassword implements Response{
	
	private DatabaseRepository repo;
	
	public ChangePassword(DatabaseRepository repo){
		this.repo = repo;
	}
	
	@RouteDoc(path = "/user/cpw", responseCode = 200, responseDescription = "L'utilisateur a mis à jours sont mots de passe")
	@RouteDoc(responseCode = 400, responseDescription = "L'utilisateur a envoyer un mots de passe invalide")

	@Route(path = "^/user/cpw$", type = RequestType.POST, needLogin = true)
	public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
		String password = reader.<JsonMap>readJson().get("password");
		
		repo.updatePassword(context.<PeerAtUser>getUser().getId(), password);
		context.response(200);
	}

}
