package com.example.agentclient.utils;

import com.example.agentclient.models.*;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.*;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * API客户端配置
 * 
 * 使用说明：
 * 1. 修改 BASE_URL 为你的服务器地址
 * 2. 确保服务器允许跨域请求（CORS）
 */
public class ApiClient {
    // TODO: 修改为你的服务器地址
    private static final String BASE_URL = "http://101.43.95.130:8037";
    
    private static Retrofit retrofit;
    private static AgentService agentService;
    
    static {
        // 创建OkHttp客户端
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .addInterceptor(logging)
                .build();
        
        // 创建Retrofit实例
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        
        // 创建API服务
        agentService = retrofit.create(AgentService.class);
    }
    
    public static AgentService getService() {
        return agentService;
    }
    
    /**
     * Agent API接口
     */
    public interface AgentService {
        /**
         * 用户登录
         * 
         * @param username 用户名
         * @param password 密码
         * @return Token响应
         */
        @FormUrlEncoded
        @POST("/api/v1/auth/login")
        retrofit2.Call<TokenResponse> login(
                @Field("username") String username,
                @Field("password") String password
        );
        
        /**
         * 获取Agent列表（支持搜索）
         * 
         * @param search 搜索关键词（Agent名称）
         * @param limit 返回数量限制
         * @param token 认证Token
         * @return Agent列表
         */
        @GET("/api/v1/agents")
        retrofit2.Call<List<Agent>> getAgents(
                @Query("search") String search,
                @Query("limit") Integer limit,
                @Header("Authorization") String token
        );
        
        /**
         * 执行Agent
         * 
         * @param request Agent执行请求
         * @param token 认证Token
         * @return 执行记录
         */
        @POST("/api/v1/executions")
        retrofit2.Call<ExecutionResponse> executeAgent(
                @Body AgentExecutionRequest request,
                @Header("Authorization") String token
        );
        
        /**
         * 获取执行状态
         * 
         * @param executionId 执行ID
         * @param token 认证Token
         * @return 执行状态
         */
        @GET("/api/v1/executions/{execution_id}/status")
        retrofit2.Call<ExecutionStatusResponse> getExecutionStatus(
                @Path("execution_id") String executionId,
                @Header("Authorization") String token
        );
        
        /**
         * 获取执行结果
         * 
         * @param executionId 执行ID
         * @param token 认证Token
         * @return 执行详情
         */
        @GET("/api/v1/executions/{execution_id}")
        retrofit2.Call<ExecutionResponse> getExecutionResult(
                @Path("execution_id") String executionId,
                @Header("Authorization") String token
        );
    }
}
