package com.pangu.application.controller;

import com.pangu.application.domain.dto.ApplicationDTO;
import com.pangu.application.domain.entity.ApiDict;
import com.pangu.application.domain.vo.ApplicationVO;
import com.pangu.application.service.IApplicationService;
import com.pangu.common.core.controller.BaseController;
import com.pangu.common.core.domain.AjaxResult;
import com.pangu.common.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 应用管理Controller
 * 
 * @author pangu
 */
@RestController
@RequestMapping("/api/application")
@RequiredArgsConstructor
@PreAuthorize("hasRole('admin')")
public class ApplicationController extends BaseController {

    private final IApplicationService applicationService;

    /**
     * 查询应用列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ApplicationDTO applicationDTO) {
        return applicationService.selectApplicationList(applicationDTO);
    }

    /**
     * 获取应用详情
     */
    @GetMapping("/{appId}")
    public AjaxResult getInfo(@PathVariable Long appId) {
        ApplicationVO applicationVO = applicationService.getApplicationById(appId);
        return success(applicationVO);
    }

    /**
     * 新增应用
     */
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ApplicationDTO applicationDTO) {
        ApplicationVO result = applicationService.insertApplication(applicationDTO);
        return success(result);
    }

    /**
     * 修改应用
     */
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody ApplicationDTO applicationDTO) {
        return toAjax(applicationService.updateApplication(applicationDTO));
    }

    /**
     * 删除应用
     */
    @DeleteMapping("/{appId}")
    public AjaxResult remove(@PathVariable Long appId) {
        return toAjax(applicationService.deleteApplication(appId));
    }

    /**
     * 重置应用密钥
     */
    @PutMapping("/resetSecret/{appId}")
    public AjaxResult resetSecret(@PathVariable Long appId) {
        String newSecret = applicationService.resetSecret(appId);
        return success(newSecret);
    }

    /**
     * 获取API接口列表
     */
    @GetMapping("/apiList")
    public AjaxResult getApiList() {
        List<ApiDict> list = applicationService.getApiList();
        return success(list);
    }
}
