package com.hclsoftware.sso.kerberos.v2.controllers;

import com.hclsoftware.sso.kerberos.dto.LoginDto;
import com.hclsoftware.sso.kerberos.dto.ResponseDto;
import com.hclsoftware.sso.kerberos.dto.TokenDto;
import com.hclsoftware.sso.kerberos.v2.response.GenericResponse;
import com.hclsoftware.sso.kerberos.v2.services.AuthServiceV2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/api/auth/v2")
public class AuthControllerV2 {

    @Autowired
    private AuthServiceV2 authService;


    @PostMapping(value = "/token", produces =  MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<GenericResponse<ResponseDto>> authenticateUser(@RequestBody LoginDto loginDto) throws IOException {
        return ResponseEntity.ok(GenericResponse.success(authService.authenticateUser(loginDto)));
    }

    @PostMapping(value = "/refresh_token", produces =  MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<GenericResponse<ResponseDto>> refresh(@RequestBody TokenDto token) throws IOException, ClassNotFoundException {
        return ResponseEntity.ok(GenericResponse.success(authService.refresh(token.getToken())));
    }


}
