/// LLM 配置模型
/// 
/// 对应 Supabase 中的 llm_config 表
class LlmConfigModel {
  final String id;
  final String name;
  final bool isActive;
  
  // API 连接
  final String apiBaseUrl;
  final String apiKey;
  final String model;
  
  // 模型参数
  final double temperature;
  final int maxTokens;
  final bool stream;
  
  // 时间戳
  final DateTime? createdAt;
  final DateTime? updatedAt;

  const LlmConfigModel({
    required this.id,
    required this.name,
    this.isActive = true,
    required this.apiBaseUrl,
    required this.apiKey,
    required this.model,
    this.temperature = 0.7,
    this.maxTokens = 2048,
    this.stream = true,
    this.createdAt,
    this.updatedAt,
  });

  factory LlmConfigModel.fromJson(Map<String, dynamic> json) {
    return LlmConfigModel(
      id: json['id'] as String,
      name: json['name'] as String,
      isActive: json['is_active'] as bool? ?? true,
      apiBaseUrl: json['api_base_url'] as String,
      apiKey: json['api_key'] as String,
      model: json['model'] as String,
      temperature: (json['temperature'] as num?)?.toDouble() ?? 0.7,
      maxTokens: json['max_tokens'] as int? ?? 2048,
      stream: json['stream'] as bool? ?? true,
      createdAt: json['created_at'] != null
          ? DateTime.parse(json['created_at'] as String)
          : null,
      updatedAt: json['updated_at'] != null
          ? DateTime.parse(json['updated_at'] as String)
          : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'is_active': isActive,
      'api_base_url': apiBaseUrl,
      'api_key': apiKey,
      'model': model,
      'temperature': temperature,
      'max_tokens': maxTokens,
      'stream': stream,
    };
  }

  /// 获取完整的 API URL（用于聊天完成）
  String get chatCompletionsUrl => '$apiBaseUrl/chat/completions';
}
