package com.seer.teach.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.seer.teach.admin.request.AdminAiApiKeyPageReq;
import com.seer.teach.admin.request.AdminAiApiKeyQueryReq;
import com.seer.teach.admin.request.AdminAiApiKeyReq;
import com.seer.teach.admin.response.AdminAiApiKeyResp;
import com.seer.teach.admin.service.AdminAiApiKeyService;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * AiApiKey控制器
 *
 * @author ZS
 * @since 2025-10-27
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/teacher/ai/api-key")
@Tag(name = "管理端 - Api Key管理")
public class AdminAiApiKeyController {

    private final AdminAiApiKeyService adminAiApiKeyService;

    @PostMapping("/paged")
    @Operation(summary = "分页查询AiApiKey")
    @SaCheckPermission("teacher:admin:ai-api-key:list")
    public ResultBean<PageListBean<AdminAiApiKeyResp>> pageList(@RequestBody AdminAiApiKeyPageReq pageRequest) {
        return ResultBean.success(adminAiApiKeyService.pageList(pageRequest));
    }

    @PostMapping("/list")
    @Operation(summary = "获取AiApiKey列表")
    @SaCheckPermission("teacher:admin:ai-api-key:list")
    public ResultBean<List<AdminAiApiKeyResp>> list(@RequestBody AdminAiApiKeyQueryReq queryRequest) {
        return ResultBean.success(adminAiApiKeyService.list(queryRequest));
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取AiApiKey")
    @SaCheckPermission("teacher:admin:ai-api-key:detail")
    public ResultBean<AdminAiApiKeyResp> getById(@PathVariable Integer id) {
        return ResultBean.success(adminAiApiKeyService.getDetail(id));
    }

    @PostMapping
    @Operation(summary = "新增AiApiKey")
    @SaCheckPermission("teacher:admin:ai-api-key:save")
    public ResultBean<Boolean> save(@RequestBody AdminAiApiKeyReq requestParam) {
        Integer userId = StpUtil.getLoginIdAsInt();
        requestParam.setOwnerId(userId);
        return ResultBean.success(adminAiApiKeyService.save(requestParam));
    }

    @PutMapping
    @Operation(summary = "更新AiApiKey")
    @SaCheckPermission("teacher:admin:ai-api-key:update")
    public ResultBean<Boolean> update(@RequestBody AdminAiApiKeyReq requestParam) {
        return ResultBean.success(adminAiApiKeyService.updateById(requestParam));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除AiApiKey")
    @SaCheckPermission("teacher:admin:ai-api-key:delete")
    public ResultBean<Boolean> delete(@PathVariable Integer id) {
        return ResultBean.success(adminAiApiKeyService.deleteById(id));
    }

    @GetMapping("/my-keys")
    @Operation(summary = "查询当前用户名下的API密钥列表")
    @SaCheckPermission("teacher:admin:ai-api-key:list")
    public ResultBean<List<AdminAiApiKeyResp>> getMyApiKeys(@RequestParam(required = false) String platform) {
        Integer userId = StpUtil.getLoginIdAsInt();
        return ResultBean.success(adminAiApiKeyService.listByOwnerId(userId, platform));
    }
}