package com.ruoyi.web.controller.api;


import com.ruoyi.system.domain.TokenRequest;
import com.ruoyi.system.service.impl.SecretKeyService;
import com.ruoyi.system.service.impl.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/auth")
public class SecurityController {

    @Autowired
    private SecretKeyService secretKeyService;

    @Autowired
    private TokenService tokenService;

    @PostMapping("/getRemoteSecretKey")
    public ResponseEntity<String> getRemoteSecretKey() {
        String key = secretKeyService.getRemoteSecretKey();
        if (key != null) {
            return ResponseEntity.ok("{\"code\": 200, \"msg\": \"操作成功\", \"data\": \"" + key + "\"}");
        } else {
            return ResponseEntity.ok("{\"code\": 500, \"msg\": \"操作失败\", \"data\": null}");
        }
    }

    @PostMapping("/getToken")
    public ResponseEntity<String> getToken(@RequestBody TokenRequest tokenRequest) {
        String token = tokenService.getToken(tokenRequest.getUsername(), tokenRequest.getPassword());
        if (token != null) {
            return ResponseEntity.ok("{\"code\": 200, \"msg\": \"获取成功\", \"data\": \"" + token + "\"}");
        } else {
            return ResponseEntity.ok("{\"code\": 500, \"msg\": \"获取失败\", \"data\": null}");
        }
    }
}
