interface FalModel {
  id: string
  name?: string
  description?: string
  category?: string
  type?: 'image' | 'video' | 'audio' | 'text'
  input_schema?: {
    properties?: Record<string, any>
  }
  pricing?: {
    per_request?: number
    per_second?: number
    per_image?: number
  }
  max_resolution?: string
  supported_formats?: string[]
  [key: string]: any
}

interface FalModelsResponse {
  models: FalModel[]
  error?: string
}

export class FalService {
  private apiKey: string
  private baseUrl = 'https://fal.run/api/v1'

  constructor() {
    this.apiKey = process.env.FAL_API_KEY || ''
    if (!this.apiKey) {
      throw new Error('FAL_API_KEY environment variable is required')
    }
  }

  async getAvailableModels(): Promise<FalModel[]> {
    try {
      // Fal.ai 可能没有公开的模型列表 API，我们可以定义一些常用的模型
      // 这里基于 Fal.ai 的文档和常见模型创建一个静态列表
      const knownModels: FalModel[] = [
        {
          id: 'fal-ai/stable-diffusion-xl',
          name: 'Stable Diffusion XL',
          description: 'High-quality text-to-image generation with SDXL',
          category: 'image-generation',
          type: 'image',
          max_resolution: '1024x1024',
          pricing: {
            per_image: 0.012
          }
        },
        {
          id: 'fal-ai/flux',
          name: 'FLUX.1 [schnell]',
          description: 'Fast, high-quality image generation',
          category: 'image-generation',
          type: 'image',
          max_resolution: '1024x1024',
          pricing: {
            per_image: 0.003
          }
        },
        {
          id: 'fal-ai/flux-pro',
          name: 'FLUX.1 [pro]',
          description: 'Professional-grade image generation',
          category: 'image-generation',
          type: 'image',
          max_resolution: '2048x2048',
          pricing: {
            per_image: 0.055
          }
        },
        {
          id: 'fal-ai/stable-video-diffusion',
          name: 'Stable Video Diffusion',
          description: 'Generate videos from images or text',
          category: 'video-generation',
          type: 'video',
          pricing: {
            per_second: 0.1
          }
        },
        {
          id: 'fal-ai/aura-sr',
          name: 'AuraSR',
          description: 'AI-powered image super resolution',
          category: 'image-enhancement',
          type: 'image',
          pricing: {
            per_image: 0.015
          }
        },
        {
          id: 'fal-ai/face-swap',
          name: 'Face Swap',
          description: 'Swap faces in images using AI',
          category: 'image-editing',
          type: 'image',
          pricing: {
            per_image: 0.05
          }
        },
        {
          id: 'fal-ai/remove-background',
          name: 'Background Removal',
          description: 'Remove backgrounds from images automatically',
          category: 'image-editing',
          type: 'image',
          pricing: {
            per_image: 0.01
          }
        },
        {
          id: 'fal-ai/lora-image-generation',
          name: 'LoRA Image Generation',
          description: 'Fine-tuned image generation with LoRA models',
          category: 'image-generation',
          type: 'image',
          pricing: {
            per_image: 0.025
          }
        },
        {
          id: 'fal-ai/realtime-stable-diffusion',
          name: 'Realtime Stable Diffusion',
          description: 'Fast, real-time image generation',
          category: 'image-generation',
          type: 'image',
          pricing: {
            per_image: 0.008
          }
        },
        {
          id: 'fal-ai/photomaker',
          name: 'PhotoMaker',
          description: 'Generate personalized photos with AI',
          category: 'image-generation',
          type: 'image',
          pricing: {
            per_image: 0.04
          }
        }
      ]

      console.log(`Fal.ai returned ${knownModels.length} predefined models`)
      return knownModels
    } catch (error) {
      console.error('Error fetching Fal models:', error)
      throw error
    }
  }

  // 将 Fal.ai 模型转换为我们数据库的格式
  transformModelForDB(model: FalModel) {
    if (!model.id) {
      return null
    }

    const modelName = model.name || model.id.split('/').pop() || model.id
    const provider = this.extractProvider(model.id)

    // 根据定价结构计算每1k的成本
    let inputCostPer1k = null
    let outputCostPer1k = null

    if (model.pricing) {
      if (model.pricing.per_image) {
        // 图像生成：假设1k token约等于1张图片
        inputCostPer1k = model.pricing.per_image * 1000
        outputCostPer1k = model.pricing.per_image * 1000
      } else if (model.pricing.per_second) {
        // 视频生成：假设1k token约等于10秒视频
        inputCostPer1k = model.pricing.per_second * 10000
        outputCostPer1k = model.pricing.per_second * 10000
      } else if (model.pricing.per_request) {
        inputCostPer1k = model.pricing.per_request * 1000
        outputCostPer1k = model.pricing.per_request * 1000
      }
    }

    return {
      modelId: model.id,
      name: modelName,
      provider: provider,
      serviceProvider: 'fal',
      outputType: model.type || 'image',
      description: model.description || null,
      maxTokens: null, // Fal.ai 模型通常不使用 token 限制
      inputCostPer1k: inputCostPer1k,
      outputCostPer1k: outputCostPer1k,
      supportedFeatures: {
        type: model.type,
        category: model.category,
        max_resolution: model.max_resolution,
        supported_formats: model.supported_formats,
      },
      metadata: {
        original: model,
        pricing_model: model.pricing
      },
    }
  }

  private extractProvider(modelId: string): string {
    // Fal.ai 模型ID格式通常是 fal-ai/model-name
    const parts = modelId.split('/')
    if (parts.length > 1) {
      const provider = parts[0]
      
      const providerMap: Record<string, string> = {
        'fal-ai': 'Fal.ai',
        'stabilityai': 'Stability AI',
        'runwayml': 'Runway ML',
        'openai': 'OpenAI',
      }
      
      return providerMap[provider] || provider.charAt(0).toUpperCase() + provider.slice(1)
    }
    
    return 'Fal.ai'
  }

  // 执行图像生成请求
  async generateImage(params: {
    model: string
    prompt: string
    image_size?: string
    num_inference_steps?: number
    guidance_scale?: number
    num_images?: number
    seed?: number
  }) {
    try {
      const response = await fetch(`${this.baseUrl}/fal-ai/${params.model.replace('fal-ai/', '')}`, {
        method: 'POST',
        headers: {
          'Authorization': `Key ${this.apiKey}`,
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          prompt: params.prompt,
          image_size: params.image_size || '1024x1024',
          num_inference_steps: params.num_inference_steps || 25,
          guidance_scale: params.guidance_scale || 7.5,
          num_images: params.num_images || 1,
          ...(params.seed && { seed: params.seed }),
        }),
      })

      if (!response.ok) {
        const errorData = await response.json().catch(() => ({}))
        throw new Error(`Fal.ai API error: ${response.status} ${response.statusText} - ${errorData.error || ''}`)
      }

      return await response.json()
    } catch (error) {
      console.error('Error calling Fal.ai image generation:', error)
      throw error
    }
  }

  // 执行视频生成请求
  async generateVideo(params: {
    model: string
    prompt?: string
    image_url?: string
    motion_bucket_id?: number
    fps?: number
    num_frames?: number
  }) {
    try {
      const response = await fetch(`${this.baseUrl}/fal-ai/${params.model.replace('fal-ai/', '')}`, {
        method: 'POST',
        headers: {
          'Authorization': `Key ${this.apiKey}`,
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          prompt: params.prompt,
          image_url: params.image_url,
          motion_bucket_id: params.motion_bucket_id || 127,
          fps: params.fps || 6,
          num_frames: params.num_frames || 25,
        }),
      })

      if (!response.ok) {
        const errorData = await response.json().catch(() => ({}))
        throw new Error(`Fal.ai API error: ${response.status} ${response.statusText} - ${errorData.error || ''}`)
      }

      return await response.json()
    } catch (error) {
      console.error('Error calling Fal.ai video generation:', error)
      throw error
    }
  }
}