import Foundation
import FoundationModels

/// Stable error codes shared with Android and declared in TypeScript as
/// `LocalLLMErrorCode`. JavaScript branches on these instead of on messages.
enum LocalLLMErrorCode: String {
  case contextWindowExceeded = "ERR_CONTEXT_WINDOW_EXCEEDED"
  case guardrailViolation = "ERR_GUARDRAIL_VIOLATION"
  case refusal = "ERR_REFUSAL"
  case unsupportedLanguage = "ERR_UNSUPPORTED_LANGUAGE"
  case unsupportedRequest = "ERR_UNSUPPORTED_REQUEST"
  case rateLimited = "ERR_RATE_LIMITED"
  case modelUnavailable = "ERR_MODEL_UNAVAILABLE"
  case decodingFailed = "ERR_DECODING_FAILED"
  case timeout = "ERR_TIMEOUT"
  case toolFailed = "ERR_TOOL_FAILED"
  case sessionBusy = "ERR_SESSION_BUSY"
  case generationFailed = "ERR_GENERATION_FAILED"
}

/// Classifies Foundation Models errors. Kept free of Expo types so the
/// model-check harness can run it against the real model.
@available(iOS 26, macOS 26, *)
enum GenerationErrorClassifier {
  static func code(for error: Error) -> LocalLLMErrorCode {
    if error is LanguageModelSession.ToolCallError {
      return .toolFailed
    }
    if let generationError = error as? LanguageModelSession.GenerationError {
      return code(for: generationError)
    }
    #if compiler(>=6.4)
    // The 27 SDKs add these types; the 27 OSes throw both them and
    // GenerationError. Older toolchains (the Xcode 26.4 floor) cannot name them.
    if #available(iOS 27, macOS 27, *), let code = code27(for: error) {
      return code
    }
    #endif
    return .generationFailed
  }

  private static func code(for error: LanguageModelSession.GenerationError) -> LocalLLMErrorCode {
    switch error {
    case .exceededContextWindowSize:
      return .contextWindowExceeded
    case .assetsUnavailable:
      return .modelUnavailable
    case .guardrailViolation:
      return .guardrailViolation
    case .unsupportedGuide:
      return .unsupportedRequest
    case .unsupportedLanguageOrLocale:
      return .unsupportedLanguage
    case .decodingFailure:
      return .decodingFailed
    case .rateLimited:
      return .rateLimited
    case .concurrentRequests:
      return .sessionBusy
    case .refusal:
      return .refusal
    @unknown default:
      return .generationFailed
    }
  }

  #if compiler(>=6.4)
  @available(iOS 27, macOS 27, *)
  private static func code27(for error: Error) -> LocalLLMErrorCode? {
    if let languageModelError = error as? LanguageModelError {
      switch languageModelError {
      case .contextSizeExceeded:
        return .contextWindowExceeded
      case .rateLimited:
        return .rateLimited
      case .guardrailViolation:
        return .guardrailViolation
      case .refusal:
        return .refusal
      case .unsupportedCapability, .unsupportedTranscriptContent, .unsupportedGenerationGuide:
        return .unsupportedRequest
      case .unsupportedLanguageOrLocale:
        return .unsupportedLanguage
      case .timeout:
        return .timeout
      @unknown default:
        return .generationFailed
      }
    }
    if error is SystemLanguageModel.Error {
      return .modelUnavailable
    }
    if let sessionError = error as? LanguageModelSession.Error {
      switch sessionError {
      case .concurrentRequests:
        return .sessionBusy
      default:
        return .generationFailed
      }
    }
    if error is GeneratedContent.ParsingError {
      return .decodingFailed
    }
    return nil
  }
  #endif
}
