/**
 * SDK API Client — shared types, interfaces, and configuration constants.
 */

import { SDKState, CheckpointStatus } from './types'

// =============================================================================
// TYPES
// =============================================================================

export interface APIResponse<T> {
  success: boolean
  data?: T
  error?: string
  version?: number
  lastModified?: string
}

export interface StateResponse {
  tenantId: string
  state: SDKState
  version: number
  lastModified: string
}

export interface SaveStateRequest {
  tenantId: string
  state: SDKState
  version?: number // For optimistic locking
}

export interface CheckpointValidationResult {
  checkpointId: string
  passed: boolean
  errors: Array<{
    ruleId: string
    field: string
    message: string
    severity: 'ERROR' | 'WARNING' | 'INFO'
  }>
  warnings: Array<{
    ruleId: string
    field: string
    message: string
    severity: 'ERROR' | 'WARNING' | 'INFO'
  }>
  validatedAt: string
  validatedBy: string
}

export interface APIError extends Error {
  status?: number
  code?: string
  retryable: boolean
}

// =============================================================================
// CONFIGURATION
// =============================================================================

export const DEFAULT_BASE_URL = '/api/sdk/v1'
export const DEFAULT_TIMEOUT = 30000 // 30 seconds
export const MAX_RETRIES = 3
export const RETRY_DELAYS = [1000, 2000, 4000] // Exponential backoff

// =============================================================================
// FETCH CONTEXT — passed to domain helpers
// =============================================================================

/**
 * Subset of the SDKApiClient that domain helpers need to make requests.
 * Avoids exposing the entire class and keeps helpers unit-testable.
 */
export interface FetchContext {
  baseUrl: string
  tenantId: string
  projectId: string | undefined
  fetchWithRetry<T>(url: string, options: RequestInit, retries?: number): Promise<T>
  fetchWithTimeout(url: string, options: RequestInit, requestId: string): Promise<Response>
  createError(message: string, status?: number, retryable?: boolean): APIError
}

// Re-export types that domain helpers need from ./types
export type { SDKState, CheckpointStatus }
