/**
 * GeoEdu Service - TypeScript Types
 * Types for the geography learning platform
 */

// Geographic types
export interface Position {
  latitude: number
  longitude: number
  altitude?: number
}

export interface Bounds {
  west: number
  south: number
  east: number
  north: number
}

export interface GeoJSONPolygon {
  type: 'Polygon'
  coordinates: number[][][]
}

// AOI (Area of Interest) types
export type AOIStatus = 'queued' | 'processing' | 'completed' | 'failed'
export type AOIQuality = 'low' | 'medium' | 'high'
export type AOITheme =
  | 'topographie'
  | 'landnutzung'
  | 'orientierung'
  | 'geologie'
  | 'hydrologie'
  | 'vegetation'

export interface AOIRequest {
  polygon: GeoJSONPolygon
  theme: AOITheme
  quality: AOIQuality
}

export interface AOIResponse {
  aoi_id: string
  status: AOIStatus
  area_km2: number
  estimated_size_mb: number
  message?: string
  download_url?: string
  manifest_url?: string
  created_at?: string
  completed_at?: string
}

export interface AOIManifest {
  version: string
  aoi_id: string
  created_at: string
  bounds: Bounds
  center: Position
  area_km2: number
  theme: AOITheme
  quality: AOIQuality
  assets: {
    terrain: { file: string; config: string }
    osm_features: { file: string }
    learning_positions: { file: string }
    attribution: { file: string }
  }
  unity: {
    coordinate_system: string
    scale: number
    terrain_resolution: number
  }
}

// Learning Node types
export type NodeType = 'question' | 'observation' | 'exploration'
export type Difficulty = 'leicht' | 'mittel' | 'schwer'

export interface LearningNode {
  id: string
  aoi_id: string
  title: string
  theme: AOITheme
  position: Position
  question: string
  hints: string[]
  answer: string
  explanation: string
  node_type: NodeType
  points: number
  approved: boolean
  media?: {
    type: 'image' | 'audio' | 'video'
    url: string
  }[]
  tags?: string[]
  difficulty?: Difficulty
  grade_level?: string
}

export interface LearningNodeRequest {
  aoi_id: string
  theme: AOITheme
  difficulty: Difficulty
  node_count: number
  grade_level?: string
  language?: string
}

export interface LearningNodeResponse {
  aoi_id: string
  theme: string
  nodes: LearningNode[]
  total_count: number
  generation_model: string
}

// Theme template types
export interface ThemeTemplate {
  id: AOITheme
  name: string
  description: string
  icon: string
  grade_levels: string[]
  example_questions: string[]
  keywords: string[]
}

export interface LearningTemplates {
  themes: ThemeTemplate[]
  difficulties: {
    id: Difficulty
    name: string
    description: string
  }[]
  supported_languages: string[]
}

// Attribution types
export interface AttributionSource {
  name: string
  license: string
  url: string
  attribution: string
  required: boolean
  logo_url?: string
}

export interface Attribution {
  sources: AttributionSource[]
  generated_at: string
  notice: string
}

// Tile metadata types
export interface TileMetadata {
  name: string
  description: string
  format: string
  scheme: string
  minzoom: number
  maxzoom: number
  bounds: [number, number, number, number]
  center: [number, number, number]
  attribution: string
  data_available: boolean
  last_updated?: string
}

export interface DEMMetadata {
  name: string
  description: string
  resolution_m: number
  coverage: string
  bounds: [number, number, number, number]
  vertical_datum: string
  horizontal_datum: string
  license: string
  attribution: string
  data_available: boolean
  tiles_generated: number
}

// Service health status
export interface GeoServiceHealth {
  status: 'healthy' | 'degraded' | 'unhealthy'
  service: string
  version: string
  environment: string
  data_status: {
    pmtiles_available: boolean
    dem_available: boolean
    tile_cache_dir: boolean
    bundle_dir: boolean
  }
  config: {
    max_aoi_size_km2: number
    supported_themes: AOITheme[]
  }
}

// Map style types (for MapLibre)
export interface MapStyle {
  version: number
  name: string
  metadata: {
    description: string
    attribution: string
  }
  sources: {
    [key: string]: {
      type: string
      tiles?: string[]
      url?: string
      minzoom?: number
      maxzoom?: number
      attribution?: string
      tileSize?: number
    }
  }
  layers: MapLayer[]
  terrain?: {
    source: string
    exaggeration: number
  }
}

export interface MapLayer {
  id: string
  type: string
  source?: string  // Optional for background layers
  'source-layer'?: string
  minzoom?: number
  maxzoom?: number
  filter?: unknown[]  // MapLibre filter expressions can have mixed types
  layout?: Record<string, unknown>
  paint?: Record<string, unknown>
}

// UI State types
export interface GeoLernweltState {
  // Current AOI
  currentAOI: AOIResponse | null
  drawnPolygon: GeoJSONPolygon | null

  // Selected theme and settings
  selectedTheme: AOITheme
  quality: AOIQuality
  difficulty: Difficulty

  // Learning nodes
  learningNodes: LearningNode[]
  selectedNode: LearningNode | null

  // UI state
  isDrawing: boolean
  isLoading: boolean
  error: string | null

  // Unity viewer state
  unityReady: boolean
  unityProgress: number
}

// API response wrapper
export interface ApiResponse<T> {
  data?: T
  error?: string
  message?: string
}

// Demo templates
export interface DemoTemplate {
  name: string
  description: string
  polygon: GeoJSONPolygon
  center: [number, number]
  area_km2: number
  suggested_themes: AOITheme[]
  features: string[]
}
