package ovh.herisson.Clyde.Services;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import ovh.herisson.Clyde.Repositories.CurriculumRepository;
import ovh.herisson.Clyde.Repositories.InscriptionRepository;
import ovh.herisson.Clyde.Repositories.UserCurriculumRepository;
import ovh.herisson.Clyde.Repositories.UserRepository;
import ovh.herisson.Clyde.Tables.InscriptionRequest;
import ovh.herisson.Clyde.Tables.RequestState;
import ovh.herisson.Clyde.Tables.User;
import ovh.herisson.Clyde.Tables.UserCurriculum;

@Service
public class InscriptionService {

    private final InscriptionRepository inscriptionRepo;

    private final UserRepository userRepo;

    private final UserCurriculumRepository userCurriculumRepo;

    private final CurriculumRepository curriculumRepo;

    private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();


    public InscriptionService(InscriptionRepository inscriptionRepo, UserRepository userRepo, UserCurriculumRepository userCurriculumRepo, CurriculumRepository curriculumRepo){
        this.inscriptionRepo = inscriptionRepo;
        this.userRepo = userRepo;
        this.userCurriculumRepo = userCurriculumRepo;
        this.curriculumRepo = curriculumRepo;
    }

    public InscriptionRequest save(InscriptionRequest inscriptionRequest){
        inscriptionRequest.setPassword(passwordEncoder.encode(inscriptionRequest.getPassword()));
        return inscriptionRepo.save(inscriptionRequest);
    }

    public InscriptionRequest getById(long id){
        return inscriptionRepo.findById(id);
    }

    public Iterable<InscriptionRequest> getAll(){
        return inscriptionRepo.findAll();
    }

    public boolean modifyState(long id, RequestState requestState) {
        InscriptionRequest inscrRequest = getById(id);

        if (inscrRequest == null)
            return false;

        // if th state is the same we don't send an email
        if (requestState == inscrRequest.getState())
            return false;

        /** todo send an email to tell the poster of the inscrRequest (inscrRequest.getEmail())
         *  to notify them that the state of their request changed
         *  FooEmailFormat toSend = (String.format("Your request state changed from %s to %s"),
         *                                              inscrRequest.getState(), requestState)
         * FooEmailSender.send(toSend, inscrRequest.getEmail())
         */


        //saves the user from the request if accepted
        if (requestState == RequestState.Accepted)
        {
            if (curriculumRepo.findById(inscrRequest.getCurriculumId()) == null)
                return false;

            User userFromRequest = new User(
                    inscrRequest.getLastName(),
                    inscrRequest.getFirstName(),
                    inscrRequest.getEmail(),
                    inscrRequest.getAddress(),
                    inscrRequest.getCountry(),
                    inscrRequest.getBirthDate(),
                    inscrRequest.getProfilePicture(),
                    inscrRequest.getPassword()
            );

            userRepo.save(userFromRequest);
            userCurriculumRepo.save(new UserCurriculum(userFromRequest, curriculumRepo.findById(inscrRequest.getCurriculumId())));
        }
        inscrRequest.setState(requestState);
        save(inscrRequest);
        return true;
    }

    public void delete(InscriptionRequest toDelete) {
        inscriptionRepo.delete(toDelete);
    }
}
