package com.bizzan.bitrade.controller.system;

import com.bizzan.bitrade.controller.common.BaseAdminController;
import com.bizzan.bitrade.entity.ConfigRobot;
import com.bizzan.bitrade.entity.ConfigSymbol;
import com.bizzan.bitrade.service.ConfigRobotService;
import com.bizzan.bitrade.service.ConfigSymbolService;
import com.bizzan.bitrade.util.MessageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/system/config-robot")
@Slf4j
public class ConfigRobotController extends BaseAdminController {

    @Autowired
    private ConfigRobotService configRobotService;

    @Autowired
    private ConfigSymbolService configSymbolService;

    @PostMapping("page-query")
    public MessageResult pageQuery(
            @RequestParam(defaultValue = "1") int pageNo,
            @RequestParam(defaultValue = "15") int pageSize,
            @RequestParam(required = false) String symbol,
            @RequestParam(required = false) Integer status
    ) {
        Pageable pageable = new PageRequest(pageNo - 1, pageSize, new Sort(Sort.Direction.DESC, "updateTime"));
        Page<ConfigRobot> page = configRobotService.pageQuery(symbol, status, pageable);
        return success(page);
    }

    @GetMapping("detail")
    public MessageResult detail(@RequestParam Long id) {
        ConfigRobot robot = configRobotService.findOne(id);
        Assert.notNull(robot, "记录不存在");
        return success(robot);
    }

    /** List custom symbols (customize=1) for dropdown */
    @GetMapping("symbols")
    public MessageResult symbols() {
        List<ConfigSymbol> all = configSymbolService.findAll();
        List<String> symbols = all.stream()
                .filter(s -> s.getCustomize() != null && s.getCustomize() == 1)
                .map(ConfigSymbol::getSymbol)
                .sorted()
                .collect(Collectors.toList());
        return success(symbols);
    }

    @PostMapping("create")
    public MessageResult create(@RequestBody ConfigRobot robot) {
        if (configRobotService.findBySymbol(robot.getSymbol()) != null) {
            return error("该交易对已配置: " + robot.getSymbol());
        }
        return success(configRobotService.create(robot));
    }

    @PostMapping("update")
    public MessageResult update(@RequestBody ConfigRobot robot) {
        ConfigRobot existing = configRobotService.findOne(robot.getId());
        Assert.notNull(existing, "记录不存在");
        if (!existing.getSymbol().equals(robot.getSymbol())) {
            ConfigRobot dup = configRobotService.findBySymbol(robot.getSymbol());
            if (dup != null) {
                return error("该交易对已配置: " + robot.getSymbol());
            }
        }
        existing.setSymbol(robot.getSymbol());
        existing.setTrend(robot.getTrend());
        existing.setOpenPrice(robot.getOpenPrice());
        if (robot.getTargetPrice() != null) existing.setTargetPrice(robot.getTargetPrice());
        if (robot.getHoverRange() != null) existing.setHoverRange(robot.getHoverRange());
        if (robot.getMinChangeRate() != null) existing.setMinChangeRate(robot.getMinChangeRate());
        if (robot.getMaxChangeRate() != null) existing.setMaxChangeRate(robot.getMaxChangeRate());
        if (robot.getHoverMinChangeRate() != null) existing.setHoverMinChangeRate(robot.getHoverMinChangeRate());
        if (robot.getHoverMaxChangeRate() != null) existing.setHoverMaxChangeRate(robot.getHoverMaxChangeRate());
        if (robot.getBridgeMinRatio() != null) existing.setBridgeMinRatio(robot.getBridgeMinRatio());
        if (robot.getBridgeMaxRatio() != null) existing.setBridgeMaxRatio(robot.getBridgeMaxRatio());
        if (robot.getUpProbability() != null) existing.setUpProbability(robot.getUpProbability());
        if (robot.getIntervalMs() != null) existing.setIntervalMs(robot.getIntervalMs());
        if (robot.getKlineMaxHighRatio() != null) existing.setKlineMaxHighRatio(robot.getKlineMaxHighRatio());
        if (robot.getKlineMaxLowRatio() != null) existing.setKlineMaxLowRatio(robot.getKlineMaxLowRatio());
        if (robot.getMinuteChangeLimit() != null) existing.setMinuteChangeLimit(robot.getMinuteChangeLimit());
        existing.setStatus(robot.getStatus());
        if (robot.getRemark() != null) existing.setRemark(robot.getRemark());
        return success(configRobotService.update(existing));
    }

    @PostMapping("delete")
    public MessageResult delete(@RequestParam Long id) {
        configRobotService.delete(id);
        return success();
    }
}
