/**
 * Type definitions for Server Recommendation API
 */

export interface Env {
  AI: Ai;  // Legacy - kept for fallback
  DB: D1Database;
  CACHE: KVNamespace;
  OPENAI_API_KEY: string;
  AI_GATEWAY_URL?: string;  // Cloudflare AI Gateway URL to bypass regional restrictions
}

export interface ValidationError {
  error: string;
  missing_fields?: string[];
  invalid_fields?: { field: string; reason: string }[];
  schema: Record<string, string>;
  example: Record<string, any>;
}

export interface RecommendRequest {
  tech_stack: string[];
  expected_users: number;
  use_case: string;
  traffic_pattern?: 'steady' | 'spiky' | 'growing';
  region_preference?: string[];
  budget_limit?: number;
  provider_filter?: string[]; // Filter by specific providers (e.g., ["Linode", "Vultr"])
  lang?: 'en' | 'zh' | 'ja' | 'ko'; // Response language
}

export interface Server {
  id: number;
  provider_name: string;
  instance_id: string;
  instance_name: string;
  vcpu: number;
  memory_mb: number;
  memory_gb: number;
  storage_gb: number;
  network_speed_gbps: number | null;
  instance_family: string | null;
  gpu_count: number;
  gpu_type: string | null;
  monthly_price: number;
  currency: 'USD' | 'KRW';
  region_name: string;
  region_code: string;
}

export interface BandwidthInfo {
  included_transfer_tb: number;      // 기본 포함 트래픽 (TB/월)
  overage_cost_per_gb: number;       // 초과 비용 ($/GB)
  overage_cost_per_tb: number;       // 초과 비용 ($/TB)
  estimated_monthly_tb: number;      // 예상 월간 사용량 (TB)
  estimated_overage_tb: number;      // 예상 초과량 (TB)
  estimated_overage_cost: number;    // 예상 초과 비용 ($)
  total_estimated_cost: number;      // 총 예상 비용 (서버 + 트래픽)
  warning?: string;                  // 트래픽 관련 경고
}

export interface RecommendationResult {
  server: Server;
  score: number;
  analysis: {
    tech_fit: string;
    capacity: string;
    cost_efficiency: string;
    scalability: string;
  };
  estimated_capacity: {
    max_daily_users?: number;
    max_concurrent_users: number;
    requests_per_second: number;
  };
  bandwidth_info?: BandwidthInfo;
  benchmark_reference?: BenchmarkReference;
  vps_benchmark_reference?: {
    plan_name: string;
    geekbench_single: number;
    geekbench_multi: number;
    monthly_price_usd: number;
    performance_per_dollar: number;
  };
}

export interface BenchmarkReference {
  processor_name: string;
  benchmarks: {
    name: string;
    category: string;
    score: number;
    percentile: number;
  }[];
}

export interface BenchmarkData {
  id: number;
  processor_name: string;
  benchmark_name: string;
  category: string;
  score: number;
  percentile: number;
  cores: number | null;
}

export interface VPSBenchmark {
  id: number;
  provider_name: string;
  plan_name: string;
  cpu_type: string;
  vcpu: number;
  memory_gb: number;
  country_code: string;
  geekbench_single: number;
  geekbench_multi: number;
  geekbench_total: number;
  monthly_price_usd: number;
  performance_per_dollar: number;
  geekbench_version: string;
  gb6_single_normalized: number;
  gb6_multi_normalized: number;
}

export interface TechSpec {
  id: number;
  name: string;
  category: string;
  vcpu_per_users: number;
  vcpu_per_users_max: number | null;
  min_memory_mb: number;
  max_memory_mb: number | null;
  description: string | null;
  aliases: string | null;
  is_memory_intensive: boolean;
  is_cpu_intensive: boolean;
}

export interface BandwidthEstimate {
  monthly_gb: number;
  monthly_tb: number;
  daily_gb: number;
  category: 'light' | 'moderate' | 'heavy' | 'very_heavy';
  description: string;
  estimated_dau_min: number;  // Daily Active Users estimate (min)
  estimated_dau_max: number;  // Daily Active Users estimate (max)
  active_ratio: number;       // Active user ratio (0.0-1.0)
}

// Use case configuration for bandwidth estimation and user metrics
export interface UseCaseConfig {
  category: 'video' | 'file' | 'gaming' | 'api' | 'ecommerce' | 'forum' | 'blog' | 'chat' | 'default';
  patterns: RegExp;
  dauMultiplier: { min: number; max: number };
  activeRatio: number;
}

export interface AIRecommendationResponse {
  recommendations: Array<{
    server_id: number;
    score: number;
    analysis: {
      tech_fit: string;
      capacity: string;
      cost_efficiency: string;
      scalability: string;
    };
    estimated_capacity: {
      max_daily_users?: number;
      max_concurrent_users: number;
      requests_per_second: number;
    };
  }>;
  infrastructure_tips?: string[];
}
