package cn.reghao.tnb.auth.app.controller;

import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.auth.app.model.dto.OAuthAppDto;
import cn.reghao.tnb.auth.app.model.po.oauth.ClientDetails;
import cn.reghao.tnb.auth.app.service.OAuthAppService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "OAuth 应用接口")
@RestController
@RequestMapping("/api/account/oauth")
public class AccountOAuthController {
	private final OAuthAppService oAuthAppService;

	public AccountOAuthController(OAuthAppService oAuthAppService) {
		this.oAuthAppService = oAuthAppService;
	}

	@Operation(summary = "创建 OAuth 应用", description = "N")
	@PostMapping(value = "/create")
	public String createOAuthApp(@RequestBody @Validated OAuthAppDto oAuthAppDto) {
		oAuthAppService.create(oAuthAppDto);
		return WebResult.success();
	}

	@Operation(summary = "更新 OAuth 应用", description = "N")
	@PostMapping(value = "/update", produces = MediaType.APPLICATION_JSON_VALUE)
	public String updateOAuthApp(@RequestBody @Validated OAuthAppDto oAuthAppDto) {
		oAuthAppService.create(oAuthAppDto);
		return WebResult.success();
	}

	@Operation(summary = "删除 OAuth 应用", description = "N")
	@PostMapping("/delete/{appId}")
	public String delete(@PathVariable String appId) {
		oAuthAppService.delete(appId);
		return WebResult.success();
	}

	@Operation(summary = "OAuth 应用列表", description = "N")
	@GetMapping("/list")
	public String getOAuthApps() {
		List<ClientDetails> list = oAuthAppService.getAll();
		return WebResult.success(list);
	}

	@Operation(summary = "OAuth 应用详情", description = "N")
	@GetMapping(value = "/view/{appName}")
	public String getOAuthApp(@PathVariable("appName") String appName) {
		ClientDetails clientDetails = oAuthAppService.get(appName);
		return WebResult.success(clientDetails);
	}
}
