/** Subset of Magento 2 REST DTOs this project reads. Field names follow Magento exactly. */

export interface SearchResult<T> {
  items: T[] | null
  total_count: number
  search_criteria?: unknown
}

export interface CustomAttribute {
  attribute_code: string
  value: string | string[] | null
}

export interface StoreView {
  id: number
  code: string
  name: string
  website_id: number
  store_group_id: number
  is_active: number
}

export interface StoreConfig {
  id: number
  code: string
  website_id: number
  locale: string
  base_currency_code: string
  default_display_currency_code: string
  timezone: string
  base_url: string
  base_media_url: string
}

export interface Website {
  id: number
  code: string
  name: string
}

export interface ProductAttribute {
  attribute_id: number
  attribute_code: string
  frontend_input: string
  is_required: boolean
  is_user_defined: boolean
  scope?: string
  default_frontend_label?: string | null
  options?: Array<{ label: string; value: string }>
}

export interface AttributeSet {
  attribute_set_id: number
  attribute_set_name: string
  entity_type_id: number
}

export interface Category {
  id: number
  parent_id: number
  name: string
  is_active: boolean
  position: number
  level: number
  path: string
  include_in_menu: boolean
  updated_at?: string
  custom_attributes?: CustomAttribute[]
}

export interface MediaGalleryEntry {
  id: number
  media_type: string
  label: string | null
  position: number
  disabled: boolean
  types: string[]
  file: string
}

export interface ProductLink {
  sku: string
  link_type: string
  linked_product_sku: string
  linked_product_type: string
  position: number
}

export interface TierPrice {
  customer_group_id: number
  qty: number
  value: number
  extension_attributes?: { website_id?: number; percentage_value?: number }
}

export interface Product {
  id: number
  sku: string
  name: string
  attribute_set_id: number
  price?: number
  status: number
  visibility: number
  type_id: string
  created_at: string
  updated_at: string
  weight?: number
  extension_attributes?: {
    website_ids?: number[]
    category_links?: Array<{ category_id: string; position: number }>
    configurable_product_links?: number[]
    bundle_product_options?: Array<{ product_links?: Array<{ sku: string }> }>
    stock_item?: { qty: number | null; is_in_stock: boolean }
  }
  product_links?: ProductLink[]
  media_gallery_entries?: MediaGalleryEntry[]
  tier_prices?: TierPrice[]
  custom_attributes?: CustomAttribute[]
}

export interface SourceItem {
  sku: string
  source_code: string
  quantity: number
  status: number
}

export interface Address {
  firstname?: string
  lastname?: string
  company?: string
  street?: string[]
  city?: string
  region?: string
  postcode?: string
  country_id?: string
  telephone?: string
  email?: string
}

export interface OrderItem {
  item_id: number
  sku: string
  name: string
  product_type: string
  parent_item_id?: number
  qty_ordered: number
  qty_invoiced: number
  qty_shipped: number
  qty_refunded: number
  qty_canceled: number
  price: number
  row_total_incl_tax?: number
}

export interface Order {
  entity_id: number
  increment_id: string
  state: string
  status: string
  created_at: string
  updated_at: string
  store_id: number
  customer_email: string
  customer_firstname?: string
  customer_lastname?: string
  customer_is_guest?: number
  grand_total: number
  subtotal: number
  shipping_amount: number
  discount_amount: number
  tax_amount: number
  total_paid?: number
  total_refunded?: number
  order_currency_code: string
  shipping_description?: string
  items: OrderItem[]
  billing_address?: Address
  payment?: { method: string; additional_information?: string[] }
  status_histories?: Array<{ created_at: string; status: string | null; comment: string | null; is_customer_notified: number | null }>
  extension_attributes?: {
    shipping_assignments?: Array<{ shipping?: { address?: Address; method?: string } }>
  }
}

export interface Invoice {
  entity_id: number
  increment_id: string
  order_id: number
  state: number
  grand_total: number
  created_at: string
}

export interface Shipment {
  entity_id: number
  increment_id: string
  order_id: number
  created_at: string
  tracks?: Array<{ track_number: string; title: string; carrier_code: string; created_at: string }>
  items?: Array<{ sku: string; name: string; qty: number }>
}

/** Read a custom attribute value from a product/category. */
export function attr(entity: { custom_attributes?: CustomAttribute[] }, code: string): string | string[] | null {
  return entity.custom_attributes?.find(a => a.attribute_code === code)?.value ?? null
}

export function attrString(entity: { custom_attributes?: CustomAttribute[] }, code: string): string | null {
  const v = attr(entity, code)
  return Array.isArray(v) ? v.join(',') : v
}

export const PRODUCT_STATUS = { ENABLED: 1, DISABLED: 2 } as const
export const VISIBILITY = { NOT_VISIBLE: 1, CATALOG: 2, SEARCH: 3, CATALOG_SEARCH: 4 } as const
export const VISIBILITY_LABEL: Record<number, string> = {
  1: 'Not Visible Individually',
  2: 'Catalog',
  3: 'Search',
  4: 'Catalog, Search',
}
