package ovh.herisson.Clyde.EndPoints;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import ovh.herisson.Clyde.Responses.UnauthorizedResponse;
import ovh.herisson.Clyde.Services.AuthenticatorService;
import ovh.herisson.Clyde.Services.TokenService;
import ovh.herisson.Clyde.Tables.Role;
import ovh.herisson.Clyde.Tables.Token;

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

    private final TokenService tokenServ;

    private final AuthenticatorService authServ;

    public TokenController(TokenService tokenServ, AuthenticatorService authServ){
        this.tokenServ = tokenServ;
        this.authServ = authServ;
    }


    @GetMapping("/tokens")
    public ResponseEntity<Iterable<Token>> getTokens(@RequestHeader("Authorization")String token){

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

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