/**
 * 前端云服务器控制四层架构统一接口定义
 * 根据文档 ARCHIVE/01_Architecture/12_Operation_Agent.md 规范实现
 * 
 * 四层架构：
 * 1. API模拟层 - 主要方案
 * 2. WEB远程控制层 - 中间层
 * 3. 远程桌面层 - 第一层兜底
 * 4. 云服务商API层 - 第二层兜底
 */

import { http } from './http';

export interface ControlCommand {
  type: 'api' | 'web' | 'desktop' | 'cloud';
  target: string;
  action: string;
  parameters: Record<string, unknown>;
  priority: 'high' | 'medium' | 'low';
  timeout?: number;
}

export interface ControlResult {
  success: boolean;
  data?: unknown;
  error?: string;
  layerUsed: string;
  duration: number;
  timestamp: number;
}

export interface LayerStatus {
  layer: string;
  available: boolean;
  latency: number;
  lastUsed: number;
  errorCount: number;
  successRate: number;
}

export class CloudControlLayerService {
  static async executeCommand(command: ControlCommand): Promise<ControlResult> {
    try {
      const response = await http.post('/api/cloud-control/execute', command);
      return response.data;
    } catch (error: any) {
      throw new Error(`控制命令执行失败: ${error.message}`);
    }
  }
  
  static async getLayerStatus(): Promise<LayerStatus[]> {
    try {
      const response = await http.get('/api/cloud-control/status');
      return response.data;
    } catch (error: any) {
      throw new Error(`获取层级状态失败: ${error.message}`);
    }
  }
  
  static async testLayer(layer: string): Promise<boolean> {
    try {
      const response = await http.post(`/api/cloud-control/test/${layer}`);
      return response.data.success;
    } catch (error) {
      return false;
    }
  }
  
  static async getLayerStats(): Promise<{
    totalCommands: number;
    successRate: number;
    averageLatency: number;
    layerUsage: Record<string, number>;
  }> {
    try {
      const response = await http.get('/api/cloud-control/stats');
      return response.data;
    } catch (error: any) {
      throw new Error(`获取层级统计失败: ${error.message}`);
    }
  }
}

export class ApiSimulationLayer {
  static async simulateApiCall(platform: string, action: string, params: Record<string, unknown>): Promise<ControlResult> {
    const command: ControlCommand = {
      type: 'api',
      target: platform,
      action,
      parameters: params,
      priority: 'high',
    };
    
    return await CloudControlLayerService.executeCommand(command);
  }
}

export class WebRemoteLayer {
  static async executeWebRemote(platform: string, action: string, params: Record<string, unknown>): Promise<ControlResult> {
    const command: ControlCommand = {
      type: 'web',
      target: platform,
      action,
      parameters: params,
      priority: 'medium',
    };
    
    return await CloudControlLayerService.executeCommand(command);
  }
}

export class RemoteDesktopLayer {
  static async executeRemoteDesktop(instanceId: string, action: string, params: Record<string, unknown>): Promise<ControlResult> {
    const command: ControlCommand = {
      type: 'desktop',
      target: instanceId,
      action,
      parameters: params,
      priority: 'low',
    };
    
    return await CloudControlLayerService.executeCommand(command);
  }
}

export class CloudProviderLayer {
  static async executeCloudApi(provider: string, action: string, params: Record<string, unknown>): Promise<ControlResult> {
    const command: ControlCommand = {
      type: 'cloud',
      target: provider,
      action,
      parameters: params,
      priority: 'low',
    };
    
    return await CloudControlLayerService.executeCommand(command);
  }
}

export class CloudControlError extends Error {
  constructor(
    public layer: string,
    public operation: string,
    public originalError?: Error
  ) {
    super(`[${layer}] ${operation} failed`);
    this.name = 'CloudControlError';
  }
}
