package com.cyksj.service.claude.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyksj.mapper.claudecode.RelayServerMapper;
import com.cyksj.model.entity.RelayServer;
import com.cyksj.service.claude.RelayService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 中继服务器服务
 */
@Service
@RequiredArgsConstructor
public class RelayServiceImpl implements RelayService {
    
    private final RelayServerMapper relayServerMapper;
    
    /**
     * 获取可用中继服务器列表
     */
    public List<RelayServer> getAvailableRelays(String domain) {
        QueryWrapper<RelayServer> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1)
                .orderByDesc("priority")
                .orderByAsc("latency_ms");
        
        if (domain != null && !domain.isEmpty()) {
            wrapper.like("host", domain);
        }
        
        return relayServerMapper.selectList(wrapper);
    }
    
    /**
     * 创建中继服务器
     */
    public void createRelayServer(RelayServer relayServer) {
        relayServerMapper.insert(relayServer);
    }
    
    /**
     * 更新中继服务器
     */
    public void updateRelayServer(RelayServer relayServer) {
        relayServerMapper.updateById(relayServer);
    }
    
    /**
     * 删除中继服务器
     */
    public void deleteRelayServer(Long id) {
        relayServerMapper.deleteById(id);
    }
}