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.Services.ProtectionService;
import ovh.herisson.Clyde.Tables.InscriptionRequest;
import ovh.herisson.Clyde.Tables.RequestState;
import ovh.herisson.Clyde.Tables.Role;
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();

        return new ResponseEntity<>(ProtectionService.requestsWithoutPasswords(inscriptionRequests), 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<>(ProtectionService.requestWithoutPassword(foundInscriptionRequest), HttpStatus.OK);
    }

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

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

        return new ResponseEntity<>(HttpStatus.OK);
    }
    @DeleteMapping("/request/register/{id}")
    public ResponseEntity<String > deleteRequest(@RequestHeader("Authorization") String token, @PathVariable Long id){
        if (authServ.isNotIn(new Role[]{Role.Admin,Role.InscriptionService}, token))
            return new UnauthorizedResponse<>(null);

        InscriptionRequest toDelete = inscriptionServ.getById(id);

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

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

}
