package ovh.herisson.Clyde.EndPoints;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import ovh.herisson.Clyde.Repositories.TokenRepository;
import ovh.herisson.Clyde.Repositories.UserRepository;
import ovh.herisson.Clyde.Services.TokenService;
import ovh.herisson.Clyde.Tables.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;

@RestController
@CrossOrigin(originPatterns = "*", allowCredentials = "true")

public class MockController {
    private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    public final UserRepository userRepo;
    public final TokenRepository tokenRepo;
    public final TokenService tokenService;

    ArrayList<User> mockUsers;


    public MockController(UserRepository userRepo, TokenRepository tokenRepo, TokenService tokenService){
        this.tokenRepo = tokenRepo;
        this.userRepo = userRepo;
        this.tokenService = tokenService;
    }

    /** Saves an example of each user type by :
     * email : FooRole@FooRole.com, password : FooRole and token : FooRole
     * For example the admin as "admin@admin.com" as email and "admin" as both password and token
     * They all have silly names
     */

    @PostMapping("/mock")
    public void postMock(){

        User herobrine = new User("brine","hero","admin@admin.com","in your WalLs","ShadowsLand",new Date(0), null,Role.Admin,passwordEncoder.encode("admin"));
        User joe = new User("Mama","Joe","student@student.com","roundabout","DaWarudo",new Date(0), null,Role.Student,passwordEncoder.encode("student"));
        User meh = new User("Inspiration","lackOf","secretary@secretary.com","a Box","the street",new Date(0), null,Role.Teacher,passwordEncoder.encode("secretary"));
        User joke = new User("CthemBalls","Lemme","teacher@teacher.com","lab","faculty",new Date(0), null,Role.Teacher,passwordEncoder.encode("teacher"));

        mockUsers = new ArrayList<User>(Arrays.asList(herobrine,joe,meh,joke));

        userRepo.saveAll(mockUsers);
    }

    @DeleteMapping("/mock")
    public void deleteMock(){
        for (User user:mockUsers){
            tokenRepo.deleteAll(tokenRepo.getByUser(user));
        }
        userRepo.deleteAll(mockUsers);
    }
}

