package ovh.herisson.Clyde.EndPoints;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ovh.herisson.Clyde.Responses.UnauthorizedResponse;
import ovh.herisson.Clyde.Services.AuthenticatorService;
import ovh.herisson.Clyde.Services.InscriptionService;
import ovh.herisson.Clyde.Tables.InscriptionRequest;
import ovh.herisson.Clyde.Tables.RequestState;
import ovh.herisson.Clyde.Tables.Role;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin(originPatterns = "*", allowCredentials = "true")
public class InscriptionController {


    private final InscriptionService inscriptionServ;
    private final AuthenticatorService authServ;

    public InscriptionController(InscriptionService inscriptionServ, AuthenticatorService authServ){
        this.inscriptionServ = inscriptionServ;
        this.authServ = authServ;
    }


    @GetMapping("/requests/register")
    public ResponseEntity<Iterable<Map<String,Object>>> getAllRequests(@RequestHeader("Authorization") String token){

        if (authServ.isNotIn(new Role[]{Role.Admin,Role.InscriptionService},token))
            return new UnauthorizedResponse<>(null);

        Iterable<InscriptionRequest> inscriptionRequests = inscriptionServ.getAll();
        ArrayList<Map<String,Object>> toReturn = new ArrayList<>();

        for (InscriptionRequest i:inscriptionRequests){
            toReturn.add(requestWithoutPassword(i));
        }

        return new ResponseEntity<>(toReturn, HttpStatus.OK);
    }


    @GetMapping("/request/register/{id}")
    public ResponseEntity<Map<String,Object>> getById(@RequestHeader("Authorization") String token, @PathVariable long id){

        if (authServ.isNotIn(new Role[]{Role.Admin,Role.InscriptionService},token))
            return new UnauthorizedResponse<>(null);

        InscriptionRequest foundInscriptionRequest = inscriptionServ.getById(id);

        if (foundInscriptionRequest == null)
            return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);

        return new ResponseEntity<>(requestWithoutPassword(foundInscriptionRequest), HttpStatus.OK);
    }

    @PatchMapping("/request/register/{id}")
    public ResponseEntity<InscriptionRequest> changeRequestState(@PathVariable long id,
                                                                 @RequestHeader("Authorization") String token,
                                                                 @RequestBody RequestState requestState)
    {
        System.out.println(requestState);
        if (authServ.isNotIn(new Role[]{Role.InscriptionService,Role.Admin},token))
            return new UnauthorizedResponse<>(null);

        if (!inscriptionServ.modifyState(id, requestState))
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);

        return new ResponseEntity<>(HttpStatus.OK);
    }

    private Map<String, Object> requestWithoutPassword(InscriptionRequest inscriptionRequest) {
        Map<String, Object> toReturn = new HashMap<>();

        toReturn.put("id", inscriptionRequest.getId());
        toReturn.put("lastName", inscriptionRequest.getLastName());
        toReturn.put("firstName", inscriptionRequest.getFirstName());
        toReturn.put("address", inscriptionRequest.getAddress());
        toReturn.put("email",inscriptionRequest.getEmail());
        toReturn.put("birthDate", inscriptionRequest.getBirthDate());
        toReturn.put("country", inscriptionRequest.getCountry());
        toReturn.put("curriculum", inscriptionRequest.getCurriculumId());
        toReturn.put("state", inscriptionRequest.getState());
        toReturn.put("profilePictureUrl", inscriptionRequest.getProfilePicture());

        return toReturn;
    }
}
