package com.ssyf.module.robot.controller.share;

import com.ssyf.common.web.result.Result;
import com.ssyf.module.robot.domain.entity.RRobotAccount;
import com.ssyf.module.robot.service.RRobotAccountService;
import com.ssyf.task.service.RobotAutomationService;
import com.ssyf.third.bmiss.model.ApiResponse;
import com.ssyf.third.bmiss.model.LoginResponse;
import com.ssyf.third.bmiss.services.BusinessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 配置关联开放接口
 *
 * @author: auth
 * @date: 2025-12-18
 */
@Api(tags="测试接口")
@RestController
@RequestMapping("/share/test")
public class TestShareController {

    @Autowired
    private RobotAutomationService robotAutomationService;

    @Autowired
    private RRobotAccountService robotAccountService;

    @Autowired
    private BusinessService businessService;

    /**
     * 新增配置关联
     */
    @ApiOperation("重启任务")
    @PostMapping("/restartTask")
    public Result restartTask() {
        this.robotAutomationService.reset();
        return Result.success();
    }

    /**
     * 新增配置关联
     */
    @ApiOperation("生成机器人的token")
    @PostMapping("/createRobotToken")
    public Result createRobotToken() {
        List<RRobotAccount> accountList = robotAccountService.list();
        List<String> tokens = new ArrayList<>();
        for (RRobotAccount account : accountList){
            ApiResponse<LoginResponse> response = businessService.loginByEmail(account.getEmail(), account.getPassword());
            if(response.isSuccess()){
                String token = response.getData().getToken();
                tokens.add(token);
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

        return Result.success(tokens);
    }

}