/**
 * Type definitions for GBFS v3.1-RC
 * Specification: https://github.com/MobilityData/gbfs/blob/v3.1-RC/gbfs.md
 */

/**
 * Base GBFS response wrapper
 */
export interface GBFSResponse<T> {
  last_updated: string // RFC3339 timestamp
  ttl: number // Time to live in seconds
  version: string // GBFS version (e.g., "3.1-RC")
  data: T
}

/**
 * Localized string for multilingual support
 */
export interface LocalizedString {
  text: string
  language: string // IETF BCP 47 language code
}

/**
 * Localized URL for multilingual support
 */
export interface LocalizedURL {
  text: string // URL
  language: string // IETF BCP 47 language code
}

// ============================================================================
// gbfs.json - Auto-discovery file
// ============================================================================

export interface GBFSData {
  feeds: GBFSFeed[]
}

export interface GBFSFeed {
  name: string // Feed type name
  url: string // Feed URL
}

// ============================================================================
// system_information.json
// ============================================================================

export interface SystemInformation {
  system_id: string
  languages: string[] // Array of IETF BCP 47 language codes
  name: LocalizedString[]
  short_name?: LocalizedString[]
  operator?: LocalizedString[]
  url?: string
  purchase_url?: LocalizedURL[]
  start_date?: string // YYYY-MM-DD
  termination_date?: string // YYYY-MM-DD
  phone_number?: string // E.164 format
  email?: string
  feed_contact_email?: string
  timezone: string // TZ timezone
  license_id?: string // SPDX identifier
  license_url?: string
  attribution_organization_name?: LocalizedString[]
  attribution_url?: LocalizedURL[]
  brand_assets?: BrandAssets
  terms_url?: LocalizedURL[]
  terms_last_updated?: string // YYYY-MM-DD
  privacy_url?: LocalizedURL[]
  privacy_last_updated?: string // YYYY-MM-DD
  rental_apps?: RentalApps
  opening_hours?: string // OSM opening_hours format
}

export interface BrandAssets {
  brand_last_modified: string // YYYY-MM-DD
  brand_image_url: string
  brand_image_url_dark?: string
  color?: string // Hex color
  brand_terms_url?: string
}

export interface RentalApps {
  android?: RentalApp
  ios?: RentalApp
}

export interface RentalApp {
  discovery_uri?: string
  store_uri: string
}

// ============================================================================
// station_information.json
// ============================================================================

export interface StationInformationData {
  stations: Station[]
}

export interface Station {
  station_id: string
  name: LocalizedString[]
  short_name?: LocalizedString[]
  lat: number
  lon: number
  address?: string
  cross_street?: string
  region_id?: string
  post_code?: string
  city?: string
  station_opening_hours?: string // OSM opening_hours format
  rental_methods?: RentalMethod[]
  is_virtual_station?: boolean
  station_area?: GeoJSONMultiPolygon
  parking_type?: ParkingType
  parking_hoop?: boolean
  contact_phone?: string
  capacity?: number
  vehicle_types_capacity?: VehicleTypeCapacity[]
  vehicle_docks_capacity?: VehicleDocksCapacity[]
  is_valet_station?: boolean
  is_charging_station?: boolean
  rental_uris?: RentalUris
}

export type RentalMethod = 'key' | 'creditcard' | 'paypass' | 'applepay' | 'androidpay' | 'transitcard' | 'accountnumber' | 'phone'

export type ParkingType = 'parking_lot' | 'street_parking' | 'underground_parking' | 'sidewalk_parking' | 'other'

export interface VehicleTypeCapacity {
  vehicle_type_ids: string[]
  count: number
}

export interface VehicleDocksCapacity {
  vehicle_type_ids: string[]
  count: number
}

export interface RentalUris {
  android?: string
  ios?: string
  web?: string
}

export interface GeoJSONMultiPolygon {
  type: 'MultiPolygon'
  coordinates: number[][][][]
}

// ============================================================================
// station_status.json
// ============================================================================

export interface StationStatusData {
  stations: StationStatus[]
}

export interface StationStatus {
  station_id: string
  num_vehicles_available: number
  vehicle_types_available?: VehicleTypeAvailable[]
  num_vehicles_disabled?: number
  num_docks_available?: number
  vehicle_docks_available?: VehicleDocksAvailable[]
  num_docks_disabled?: number
  is_installed: boolean
  is_renting: boolean
  is_returning: boolean
  last_reported: string // RFC3339 timestamp
}

export interface VehicleTypeAvailable {
  vehicle_type_id: string
  count: number
}

export interface VehicleDocksAvailable {
  vehicle_type_ids: string[]
  count: number
}

// ============================================================================
// vehicle_status.json
// ============================================================================

export interface VehicleStatusData {
  vehicles: Vehicle[]
}

export interface Vehicle {
  vehicle_id: string
  lat: number
  lon: number
  is_reserved: boolean
  is_disabled: boolean
  rental_uris?: RentalUris
  vehicle_type_id?: string
  last_reported: string // RFC3339 timestamp
  current_range_meters?: number
  current_fuel_percent?: number
  station_id?: string
  home_station_id?: string
  pricing_plan_id?: string
  vehicle_equipment?: VehicleEquipment[]
  available_until?: string // RFC3339 timestamp
}

export type VehicleEquipment = 'child_seat_a' | 'child_seat_b' | 'child_seat_c' | 'winter_tires' | 'snow_chains'

// ============================================================================
// vehicle_types.json
// ============================================================================

export interface VehicleTypesData {
  vehicle_types: VehicleType[]
}

export interface VehicleType {
  vehicle_type_id: string
  form_factor: FormFactor
  rider_capacity?: number
  cargo_volume_capacity?: number
  cargo_load_capacity?: number
  propulsion_type: PropulsionType
  eco_labels?: EcoLabel[]
  max_range_meters?: number
  name?: LocalizedString[]
  vehicle_accessories?: VehicleAccessory[]
  g_co2_km?: number
  vehicle_image?: string
  make?: LocalizedString[]
  model?: LocalizedString[]
  color?: string
  description?: LocalizedString[]
  wheel_count?: number
  max_permitted_speed?: number
  rated_power?: number
  default_reserve_time?: number
  return_constraint?: ReturnConstraint
  vehicle_assets?: VehicleAssets
  default_pricing_plan_id?: string
  pricing_plan_ids?: string[]
}

export type FormFactor = 'bicycle' | 'cargo_bicycle' | 'car' | 'moped' | 'scooter_standing' | 'scooter_seated' | 'other'

export type PropulsionType = 'human' | 'electric_assist' | 'electric' | 'combustion' | 'combustion_diesel' | 'hybrid' | 'plug_in_hybrid' | 'hydrogen_fuel_cell'

export type ReturnConstraint = 'free_floating' | 'roundtrip_station' | 'any_station' | 'hybrid'

export interface EcoLabel {
  country_code: string
  eco_sticker: string
}

export type VehicleAccessory = 'air_conditioning' | 'automatic' | 'manual' | 'convertible' | 'cruise_control' | 'doors_2' | 'doors_3' | 'doors_4' | 'doors_5' | 'navigation'

export interface VehicleAssets {
  icon_url: string
  icon_url_dark?: string
  icon_last_modified: string // YYYY-MM-DD
}

// ============================================================================
// system_pricing_plans.json
// ============================================================================

export interface SystemPricingPlansData {
  plans: PricingPlan[]
}

export interface PricingPlan {
  plan_id: string
  url?: LocalizedURL[]
  name: LocalizedString[]
  currency: string // ISO 4217 currency code
  price: number
  is_taxable: boolean
  description: LocalizedString[]
  per_km_pricing?: PricingSegment[]
  per_min_pricing?: PricingSegment[]
  surge_pricing?: boolean
}

export interface PricingSegment {
  start: number
  rate: number
  interval: number
  end?: number
}

// ============================================================================
// system_regions.json
// ============================================================================

export interface SystemRegionsData {
  regions: Region[]
}

export interface Region {
  region_id: string
  name: LocalizedString[]
}

// ============================================================================
// system_alerts.json
// ============================================================================

export interface SystemAlertsData {
  alerts: Alert[]
}

export interface Alert {
  alert_id: string
  type: AlertType
  times?: AlertTime[]
  station_ids?: string[]
  region_ids?: string[]
  url?: LocalizedURL[]
  summary: LocalizedString[]
  description?: LocalizedString[]
  last_updated?: string // RFC3339 timestamp
}

export type AlertType = 'system_closure' | 'station_closure' | 'station_move' | 'other'

export interface AlertTime {
  start: string // RFC3339 timestamp
  end?: string // RFC3339 timestamp
}

// ============================================================================
// Utility Types
// ============================================================================

export interface BikeCount {
  available: number
  disabled: number
}
