package com.hr.controller;

import com.github.pagehelper.PageInfo;
import com.hr.param.AccountInterfaceParam;
import com.hr.param.InterfaceConfigParam;
import com.hr.param.IpInterfaceParam;
import com.hr.param.SwitchStatusParam;
import com.hr.service.IpTokenInterfaceService;
import com.hr.service.IpInterfaceService;
import com.hr.vo.*;
import com.yy.basedevelop.common.data.BasePageResult;
import com.yy.basedevelop.common.data.BaseReturnDto;
import com.yy.basedevelop.common.http.config.TokenUtil;
import com.yy.basedevelop.dto.LoginUser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * 接口管理
 * @author : longhoo
 * @date : 2025-10-10
 */
@RestController
@RequestMapping("/ipInterface")
@Tag(name = "接口管理")
public class IpInterfaceController {
    @Autowired
    private IpInterfaceService ipInterfaceService;
    @Autowired
    private IpTokenInterfaceService ipTokenInterfaceService;
    
    /** 
     * 分页查询
     *
     * @param ipInterfaceParam 筛选条件
     * @return 查询结果
     */
    @Operation(summary = "分页查询")
    @GetMapping("/list")
    public Mono<BasePageResult<IpInterfaceVO>> paginQuery(IpInterfaceParam ipInterfaceParam){
        BasePageResult<IpInterfaceVO> ipAccountPOS = ipInterfaceService.paginQuery(ipInterfaceParam, ipInterfaceParam.getPageNum(), ipInterfaceParam.getPageSize());
        return Mono.just(ipAccountPOS);
    }
    
    /** 
     * 新增数据
     *
     * @param ipInterfaceParam 实例对象
     * @return 实例对象
     */
    @Operation(summary = "新增/更新数据")
    @PostMapping("/saveOrUpdate")
    public Mono<BaseReturnDto<IpAccountVO>> saveOrUpdate(@RequestBody IpInterfaceParam ipInterfaceParam){
        LoginUser user= TokenUtil.getUser();
        if(user==null || user.getUserid()==null || user.getUserid()==0L){
            throw new RuntimeException("没有找到用户信息");
        }
        ipInterfaceService.saveOrUpdate(ipInterfaceParam,user.getUsername(),user.getUserid());
        return Mono.just(BaseReturnDto.success());
    }

    /** 
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Operation(summary = "通过主键删除数据")
    @DeleteMapping("/delete/{id}")
    public Mono<BaseReturnDto<Boolean>> deleteById(@PathVariable Long id){
        LoginUser user = TokenUtil.getUser();
        return Mono.just(BaseReturnDto.success(ipInterfaceService.deleteById(id,user)));
    }

    /**
     * 修改状态
     * @param switchStatusParam 主键
     * @return 是否成功
     */
    @Operation(summary = "修改状态")
    @PostMapping("/switchStatus")
    public Mono<BaseReturnDto<Boolean>> switchStatus(@RequestBody SwitchStatusParam switchStatusParam){
        return Mono.just(BaseReturnDto.success(ipInterfaceService.switchStatus(switchStatusParam)));
    }


    /**
     * 接口下拉列表
     * @return 列表
     */
    @Operation(summary = "接口下拉列表")
    @PostMapping("/optionList")
    public Mono<BaseReturnDto<List<NameValueVO>>> optionList(){
        return Mono.just(BaseReturnDto.success(ipInterfaceService.optionList()));
    }

    /**
     * 账号配置接口
     */
    @Operation(summary = "账号配置接口")
    @PostMapping("/interfaceConfig")
    public Mono<BaseReturnDto> interfaceConfig(@RequestBody InterfaceConfigParam param){
        LoginUser user = TokenUtil.getUser();
        ipTokenInterfaceService.interfaceConfig(param,user);
        return Mono.just(BaseReturnDto.success());
    }


    /**
     * 生成 token
     */
    @Operation(summary = "生成 token")
    @PostMapping("/token/generate")
    public Mono<BaseReturnDto<SecretVO>> generateToken(){
        SecretVO vo = new SecretVO();
        String secret = RandomStringUtils.random(64, "0123456789abcdef");
        String token = RandomStringUtils.random(32, "0123456789abcdef");
        vo.setToken(token);
        vo.setSecret(secret);
        return Mono.just(BaseReturnDto.success(vo));
    }


   /**
     * 删除账号接口配置
     */
    @Operation(summary = "删除账号关联的接口")
    @DeleteMapping("/delAccountInterface/{id}")
    public Mono<BaseReturnDto> delAccountInterface(@PathVariable Long id){
        LoginUser user = TokenUtil.getUser();
        ipTokenInterfaceService.delAccountInterface(id,user);
        return Mono.just(BaseReturnDto.success());
    }

    /**
     * 查询账号下关联的接口列表
     */
    @Operation(summary = "查询账号下关联的接口列表")
    @PostMapping("/account/ref/list")
    public Mono<BasePageResult<AccountInterfaceVO>> accountInterfaceList(@RequestBody AccountInterfaceParam pageParam){
        BasePageResult<AccountInterfaceVO> result = ipTokenInterfaceService.accountInterfaceList(pageParam);
        return Mono.just(result);
    }

    /**
     * 账号关联接口修改接口状态
     * @param switchStatusParam 主键
     * @return 是否成功
     */
    @Operation(summary = "修改状态")
    @PostMapping("/account/interface/ref/switchStatus")
    public Mono<BaseReturnDto<Boolean>> accountInterfaceRefSwitch(@RequestBody SwitchStatusParam switchStatusParam){
        return Mono.just(BaseReturnDto.success(ipTokenInterfaceService.switchStatus(switchStatusParam)));
    }
}