package org.dromara.ai.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.ai.domain.bo.AiProviderConfigBo;
import org.dromara.ai.domain.vo.AiProviderConfigVo;
import org.dromara.ai.service.AiProviderConfigService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * AI Provider配置管理
 *
 * @author pengles
 */
@Tag(name = "管理端-AI Provider配置管理", description = "AI Provider配置管理接口")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ai/provider/config")
public class AdminAiProviderConfigController extends BaseController {

    private final AiProviderConfigService aiProviderConfigService;

    /**
     * 查询AI Provider配置列表
     */
    @Operation(summary = "查询AI Provider配置列表", description = "分页查询AI Provider配置列表，支持多条件搜索")
    @SaCheckPermission("ai:provider:config:list")
    @GetMapping("/list")
    public TableDataInfo<AiProviderConfigVo> list(@Parameter(description = "Provider配置查询条件") AiProviderConfigBo bo,
                                                   @Parameter(description = "分页参数") PageQuery pageQuery) {
        return aiProviderConfigService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出AI Provider配置列表
     */
    @Operation(summary = "导出AI Provider配置列表", description = "导出AI Provider配置到Excel")
    @SaCheckPermission("ai:provider:config:export")
    @Log(title = "AI Provider配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Parameter(description = "Provider配置查询条件") AiProviderConfigBo bo,
                       @Parameter(description = "响应对象") HttpServletResponse response) {
        List<AiProviderConfigVo> list = aiProviderConfigService.queryList(bo);
        ExcelUtil.exportExcel(list, "AI Provider配置", AiProviderConfigVo.class, response);
    }

    /**
     * 获取AI Provider配置详细信息
     */
    @Operation(summary = "获取AI Provider配置详情", description = "根据配置ID获取详细信息")
    @SaCheckPermission("ai:provider:config:query")
    @GetMapping("/{id}")
    public R<AiProviderConfigVo> getInfo(@Parameter(description = "配置ID", required = true)
                                          @NotNull(message = "主键不能为空")
                                          @PathVariable Long id) {
        return R.ok(aiProviderConfigService.queryById(id));
    }

    /**
     * 新增AI Provider配置
     */
    @Operation(summary = "新增AI Provider配置", description = "新增AI Provider配置")
    @SaCheckPermission("ai:provider:config:add")
    @Log(title = "AI Provider配置", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody AiProviderConfigBo bo) {
        return toAjax(aiProviderConfigService.insertByBo(bo));
    }

    /**
     * 修改AI Provider配置
     */
    @Operation(summary = "修改AI Provider配置", description = "修改AI Provider配置")
    @SaCheckPermission("ai:provider:config:edit")
    @Log(title = "AI Provider配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody AiProviderConfigBo bo) {
        return toAjax(aiProviderConfigService.updateByBo(bo));
    }

    /**
     * 批量删除AI Provider配置
     */
    @Operation(summary = "批量删除AI Provider配置", description = "根据ID数组批量删除AI Provider配置")
    @SaCheckPermission("ai:provider:config:remove")
    @Log(title = "AI Provider配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@Parameter(description = "配置ID数组", required = true)
                           @NotNull(message = "主键不能为空")
                           @PathVariable Long[] ids) {
        return toAjax(aiProviderConfigService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 启用/禁用Provider配置
     */
    @Operation(summary = "启用/禁用Provider配置", description = "启用或禁用AI Provider配置")
    @SaCheckPermission("ai:provider:config:edit")
    @Log(title = "AI Provider配置", businessType = BusinessType.UPDATE)
    @PutMapping("/changeEnabled")
    public R<Void> changeEnabled(@Parameter(description = "配置ID", required = true)
                                  @NotNull(message = "配置ID不能为空")
                                  @RequestParam Long id,
                                  @Parameter(description = "启用状态(0=禁用 1=启用)", required = true)
                                  @NotNull(message = "启用状态不能为空")
                                  @RequestParam Integer enabled) {
        return toAjax(aiProviderConfigService.changeEnabled(id, enabled));
    }

    /**
     * 根据Provider名称查询配置
     */
    @Operation(summary = "根据Provider名称查询配置", description = "根据Provider名称获取配置信息")
    @SaCheckPermission("ai:provider:config:query")
    @GetMapping("/name/{providerName}")
    public R<AiProviderConfigVo> getByProviderName(@Parameter(description = "Provider名称", required = true)
                                                     @PathVariable String providerName) {
        return R.ok(aiProviderConfigService.queryByProviderName(providerName));
    }

}
