/**
 * EasyShip API Types
 * Type definitions for EasyShip API requests and responses
 * API Version: 2024-09 (validated against live API)
 */

/**
 * EasyShip Address Format (used for origin, destination, sender, return)
 */
export interface EasyShipAddress {
  line_1: string
  line_2?: string
  city: string
  state: string
  postal_code: string
  country_alpha2: string
  company_name?: string
  contact_name: string
  contact_phone: string
  contact_email: string
}

/**
 * EasyShip Parcel Item
 */
export interface EasyShipItem {
  description: string
  sku?: string
  quantity: number
  actual_weight: number
  height?: number
  width?: number
  length?: number
  category?: string
  hs_code?: string
  declared_currency: string
  declared_customs_value: number
}

/**
 * EasyShip Parcel (contains items + box dimensions)
 */
export interface EasyShipParcel {
  box: {
    length: number
    width: number
    height: number
  }
  items: EasyShipItem[]
  total_actual_weight: number
}

/**
 * EasyShip Order Data (optional metadata)
 */
export interface EasyShipOrderData {
  platform_name?: string
  platform_order_number?: string
  seller_notes?: string
  buyer_notes?: string
}

/**
 * EasyShip Shipment Request (v2024-09)
 */
export interface EasyShipShipmentRequest {
  origin_address: EasyShipAddress
  destination_address: EasyShipAddress
  parcels: EasyShipParcel[]
  incoterms?: string
  insurance?: {
    is_insured: boolean
  }
  courier_settings?: {
    courier_service_id?: string
    allow_fallback?: boolean
    apply_shipping_rules?: boolean
  }
  order_data?: EasyShipOrderData
  metadata?: Record<string, string>
  shipping_settings?: {
    units?: {
      weight: "kg" | "lb"
      dimensions: "cm" | "in"
    }
    buy_label?: boolean
    buy_label_synchronous?: boolean
    printing_options?: {
      format?: string
      label?: string
      commercial_invoice?: string
      packing_slip?: string
    }
  }
}

/**
 * EasyShip Shipment Response (v2024-09)
 */
export interface EasyShipShipmentResponse {
  shipment: {
    easyship_shipment_id: string
    shipment_state: string
    delivery_state: string
    label_state: string
    created_at: string
    updated_at: string
    tracking_page_url: string
    order_data: {
      platform_name: string | null
      platform_order_number: string | null
      seller_notes: string | null
      buyer_notes: string | null
    }
    courier_service: {
      id: string
      name: string
      umbrella_name: string
    } | null
    origin_address: EasyShipAddress
    destination_address: EasyShipAddress
    parcels: EasyShipParcel[]
    rates: Array<{
      courier_service: {
        id: string
        name: string
        umbrella_name: string
      }
      total_charge: number
      currency: string
      min_delivery_time: number
      max_delivery_time: number
    }>
  }
  meta: {
    request_id: string
  }
}

/**
 * EasyShip Error Response
 */
export interface EasyShipError {
  error?: {
    type: string
    code: string
    message: string
    details?: any[]
    request_id?: string
  }
  message?: string
}

/**
 * EasyShip Webhook Payload
 */
export interface EasyShipWebhookPayload {
  event_type:
    | "shipment.tracking.statuschanged"
    | "shipment.tracking.checkpointscreated"
    | "shipment.label.created"
    | "shipment.label.failed"
    | "shipment.cancelled"
  easyship_shipment_id: string
  platform_order_number?: string
  tracking_number?: string
  tracking_status?: string
  courier_name?: string
}

/**
 * EasyShip Shipment Result (internal)
 */
export interface EasyShipShipmentResult {
  success: boolean
  shipmentId?: string
  platformOrderNumber?: string
  error?: string
  errorDetails?: any
}
