// BigCommerce API Types
export interface BigCommerceConfig {
  storeHash: string;
  accessToken: string;
  clientId?: string;
  clientSecret?: string;
  apiVersion?: 'v2' | 'v3';
}

export interface BigCommerceError {
  status: number;
  title: string;
  type?: string;
  errors?: Record<string, string>;
}

export interface PaginationMeta {
  pagination: {
    total: number;
    count: number;
    per_page: number;
    current_page: number;
    total_pages: number;
    links?: {
      previous?: string;
      current: string;
      next?: string;
    };
  };
}

// Product Types
export interface Product {
  id: number;
  name: string;
  type: 'physical' | 'digital';
  sku: string;
  description: string;
  weight: number;
  width?: number;
  depth?: number;
  height?: number;
  price: number;
  cost_price?: number;
  retail_price?: number;
  sale_price?: number;
  map_price?: number;
  tax_class_id?: number;
  product_tax_code?: string;
  calculated_price: number;
  categories: number[];
  brand_id?: number;
  inventory_level?: number;
  inventory_warning_level?: number;
  inventory_tracking?: string;
  fixed_cost_shipping_price?: number;
  is_free_shipping: boolean;
  is_visible: boolean;
  is_featured: boolean;
  related_products: number[];
  warranty?: string;
  bin_picking_number?: string;
  layout_file?: string;
  upc?: string;
  search_keywords?: string;
  availability?: string;
  availability_description?: string;
  gift_wrapping_options_type?: string;
  gift_wrapping_options_list?: number[];
  sort_order: number;
  condition?: string;
  is_condition_shown: boolean;
  order_quantity_minimum: number;
  order_quantity_maximum: number;
  page_title?: string;
  meta_keywords?: string[];
  meta_description?: string;
  view_count: number;
  preorder_release_date?: string;
  preorder_message?: string;
  is_preorder_only: boolean;
  is_price_hidden: boolean;
  price_hidden_label?: string;
  custom_url?: {
    url: string;
    is_customized: boolean;
  };
  open_graph_type?: string;
  open_graph_title?: string;
  open_graph_description?: string;
  open_graph_use_meta_description: boolean;
  open_graph_use_product_name: boolean;
  open_graph_use_image: boolean;
  variants?: ProductVariant[];
  images?: ProductImage[];
  custom_fields?: CustomField[];
  bulk_pricing_rules?: BulkPricingRule[];
  date_created: string;
  date_modified: string;
}

export interface ProductVariant {
  id: number;
  product_id: number;
  sku: string;
  sku_id?: number;
  price?: number;
  calculated_price?: number;
  sale_price?: number;
  retail_price?: number;
  map_price?: number;
  weight?: number;
  width?: number;
  height?: number;
  depth?: number;
  is_free_shipping: boolean;
  fixed_cost_shipping_price?: number;
  purchasing_disabled: boolean;
  purchasing_disabled_message?: string;
  image_url?: string;
  cost_price?: number;
  upc?: string;
  mpn?: string;
  gtin?: string;
  inventory_level?: number;
  inventory_warning_level?: number;
  bin_picking_number?: string;
  option_values: Array<{
    id: number;
    label: string;
    option_id: number;
    option_display_name: string;
  }>;
}

export interface ProductImage {
  id: number;
  product_id: number;
  image_file?: string;
  url_zoom?: string;
  url_standard?: string;
  url_thumbnail?: string;
  url_tiny?: string;
  date_modified?: string;
  is_thumbnail: boolean;
  sort_order: number;
  description?: string;
}

export interface CustomField {
  id: number;
  name: string;
  value: string;
}

export interface BulkPricingRule {
  id: number;
  quantity_min: number;
  quantity_max: number;
  type: 'price' | 'percent' | 'fixed';
  amount: number;
}

// Order Types
export interface Order {
  id: number;
  customer_id: number;
  date_created: string;
  date_modified: string;
  date_shipped?: string;
  status_id: number;
  status: string;
  subtotal_ex_tax: string;
  subtotal_inc_tax: string;
  subtotal_tax: string;
  base_shipping_cost: string;
  shipping_cost_ex_tax: string;
  shipping_cost_inc_tax: string;
  shipping_cost_tax: string;
  shipping_cost_tax_class_id: number;
  base_handling_cost: string;
  handling_cost_ex_tax: string;
  handling_cost_inc_tax: string;
  handling_cost_tax: string;
  handling_cost_tax_class_id: number;
  base_wrapping_cost: string;
  wrapping_cost_ex_tax: string;
  wrapping_cost_inc_tax: string;
  wrapping_cost_tax: string;
  wrapping_cost_tax_class_id: number;
  total_ex_tax: string;
  total_inc_tax: string;
  total_tax: string;
  items_total: number;
  items_shipped: number;
  payment_method: string;
  payment_provider_id?: string;
  payment_status?: string;
  refunded_amount: string;
  order_is_digital: boolean;
  store_credit_amount: string;
  gift_certificate_amount: string;
  ip_address: string;
  geoip_country?: string;
  geoip_country_iso2?: string;
  currency_id: number;
  currency_code: string;
  currency_exchange_rate: string;
  default_currency_id: number;
  default_currency_code: string;
  staff_notes?: string;
  customer_message?: string;
  discount_amount: string;
  coupon_discount: string;
  shipping_address_count: number;
  is_deleted: boolean;
  ebay_order_id?: string;
  cart_id?: string;
  billing_address: Address;
  is_email_opt_in: boolean;
  credit_card_type?: string;
  order_source?: string;
  channel_id: number;
  external_source?: string;
  products?: OrderProduct[];
  shipping_addresses?: ShippingAddress[];
  coupons?: OrderCoupon[];
  external_id?: string;
  external_merchant_id?: string;
  tax_provider_id?: string;
  customer_locale?: string;
}

export interface OrderProduct {
  id: number;
  order_id: number;
  product_id: number;
  order_address_id: number;
  name: string;
  name_customer?: string;
  name_merchant?: string;
  sku: string;
  upc?: string;
  type: string;
  base_price: string;
  price_ex_tax: string;
  price_inc_tax: string;
  price_tax: string;
  base_total: string;
  total_ex_tax: string;
  total_inc_tax: string;
  total_tax: string;
  weight: string;
  width?: string;
  height?: string;
  depth?: string;
  quantity: number;
  base_cost_price: string;
  cost_price_inc_tax: string;
  cost_price_ex_tax: string;
  cost_price_tax: string;
  is_refunded: boolean;
  quantity_refunded: number;
  refund_amount: string;
  return_id: number;
  wrapping_name?: string;
  base_wrapping_cost: string;
  wrapping_cost_ex_tax: string;
  wrapping_cost_inc_tax: string;
  wrapping_cost_tax: string;
  wrapping_message?: string;
  quantity_shipped: number;
  event_name?: string;
  event_date?: string;
  fixed_shipping_cost: string;
  ebay_item_id?: string;
  ebay_transaction_id?: string;
  option_set_id?: number;
  parent_order_product_id?: number;
  is_bundled_product: boolean;
  bin_picking_number?: string;
  external_id?: string;
  fulfillment_source?: string;
  brand?: string;
  applied_discounts?: Array<{
    id: string;
    amount: string;
  }>;
  product_options?: Array<{
    id: number;
    option_id: number;
    order_product_id: number;
    product_option_id: number;
    display_name: string;
    display_value: string;
    value: string;
    type: string;
    name: string;
    display_style: string;
  }>;
  configurable_fields?: Array<{
    name: string;
    value: string;
  }>;
}

export interface Address {
  first_name: string;
  last_name: string;
  company?: string;
  street_1: string;
  street_2?: string;
  city: string;
  state: string;
  zip: string;
  country: string;
  country_iso2: string;
  phone?: string;
  email?: string;
}

export interface ShippingAddress extends Address {
  id: number;
  order_id: number;
  items_total: number;
  items_shipped: number;
  shipping_method: string;
  base_cost: string;
  cost_ex_tax: string;
  cost_inc_tax: string;
  cost_tax: string;
  cost_tax_class_id: number;
  base_handling_cost: string;
  handling_cost_ex_tax: string;
  handling_cost_inc_tax: string;
  handling_cost_tax: string;
  handling_cost_tax_class_id: number;
  shipping_zone_id: number;
  shipping_zone_name: string;
  shipping_quotes?: ShippingQuote[];
}

export interface ShippingQuote {
  id: string;
  uuid: string;
  timestamp: string;
  shipping_provider_id: string;
  shipping_provider_quote: string;
  shipping_method_id: number;
  shipping_method: string;
  carrier_code?: string;
  carrier_service_code?: string;
}

export interface OrderCoupon {
  id: number;
  coupon_id: number;
  order_id: number;
  code: string;
  amount: string;
  type: number;
  discount: string;
}

// Customer Types
export interface Customer {
  id: number;
  company?: string;
  first_name: string;
  last_name: string;
  email: string;
  phone?: string;
  date_created: string;
  date_modified: string;
  store_credit_amounts?: Array<{
    amount: number;
  }>;
  registration_ip_address?: string;
  customer_group_id?: number;
  notes?: string;
  tax_exempt_category?: string;
  accepts_product_review_abandoned_cart_emails: boolean;
  addresses?: CustomerAddress[];
  attributes?: Array<{
    attribute_id: number;
    attribute_value: string;
    date_created: string;
    date_modified: string;
  }>;
  authentication?: {
    force_password_reset: boolean;
  };
  form_fields?: Array<{
    name: string;
    value: string;
  }>;
  channel_ids?: number[];
  origin_channel_id?: number;
}

export interface CustomerAddress {
  id: number;
  customer_id: number;
  first_name: string;
  last_name: string;
  company?: string;
  address1: string;
  address2?: string;
  city: string;
  state_or_province: string;
  postal_code: string;
  country_code: string;
  phone?: string;
  address_type: 'residential' | 'commercial';
  form_fields?: Array<{
    name: string;
    value: string;
  }>;
}

export interface CustomerGroup {
  id: number;
  name: string;
  is_default: boolean;
  category_access: {
    type: 'all' | 'specific' | 'none';
    categories?: number[];
  };
  discount_rules?: Array<{
    type: 'price_list' | 'percentage' | 'fixed';
    method: 'percent' | 'price';
    amount: string;
  }>;
  is_group_for_guests: boolean;
}

// Cart & Checkout Types
export interface Cart {
  id: string;
  customer_id: number;
  channel_id: number;
  email: string;
  currency: {
    code: string;
  };
  tax_included: boolean;
  base_amount: number;
  discount_amount: number;
  cart_amount: number;
  coupons?: Array<{
    id: string;
    code: string;
    name: string;
    discount_amount: number;
    discounted_amount: number;
  }>;
  discounts?: Array<{
    id: string;
    discounted_amount: number;
  }>;
  line_items: {
    physical_items: CartLineItem[];
    digital_items: CartLineItem[];
    gift_certificates?: CartGiftCertificate[];
    custom_items?: CartCustomItem[];
  };
  created_time: string;
  updated_time: string;
  locale: string;
}

export interface CartLineItem {
  id: string;
  parent_id?: string;
  variant_id: number;
  product_id: number;
  sku: string;
  name: string;
  url: string;
  quantity: number;
  taxable: boolean;
  image_url: string;
  discounts?: Array<{
    id: string;
    discounted_amount: number;
  }>;
  coupons?: Array<{
    code: string;
    display_name: string;
  }>;
  discount_amount: number;
  coupon_amount: number;
  list_price: number;
  sale_price: number;
  extended_list_price: number;
  extended_sale_price: number;
  is_require_shipping: boolean;
  is_mutable: boolean;
  gift_wrapping?: {
    name: string;
    message: string;
    amount: number;
  };
  options?: Array<{
    name: string;
    nameId: number;
    value: string;
    valueId: number;
  }>;
}

export interface CartGiftCertificate {
  id: string;
  name: string;
  theme: string;
  amount: number;
  taxable: boolean;
  sender: {
    name: string;
    email: string;
  };
  recipient: {
    name: string;
    email: string;
  };
  message?: string;
}

export interface CartCustomItem {
  id: string;
  sku: string;
  name: string;
  quantity: number;
  list_price: number;
}

export interface Checkout {
  id: string;
  cart: Cart;
  billing_address?: Address;
  consignments?: Array<{
    id: string;
    shipping_address: Address;
    line_items: Array<{
      item_id: string;
      quantity: number;
    }>;
    selected_shipping_option?: {
      id: string;
      type: string;
      description: string;
      image_url?: string;
      cost: number;
      transit_time?: string;
    };
    available_shipping_options?: Array<{
      id: string;
      type: string;
      description: string;
      image_url?: string;
      cost: number;
      transit_time?: string;
      additional_description?: string;
    }>;
  }>;
  order_id?: number;
  shipping_cost_total_inc_tax: number;
  shipping_cost_total_ex_tax: number;
  handling_cost_total_inc_tax: number;
  handling_cost_total_ex_tax: number;
  tax_total: number;
  subtotal_inc_tax: number;
  subtotal_ex_tax: number;
  grand_total: number;
  created_time: string;
  updated_time: string;
  customer_message?: string;
}

// Category & Brand Types
export interface Category {
  id: number;
  parent_id: number;
  name: string;
  description: string;
  views: number;
  sort_order: number;
  page_title: string;
  meta_keywords?: string[];
  meta_description?: string;
  layout_file?: string;
  image_url?: string;
  is_visible: boolean;
  search_keywords?: string;
  default_product_sort?: string;
  custom_url?: {
    url: string;
    is_customized: boolean;
  };
}

export interface Brand {
  id: number;
  name: string;
  page_title: string;
  meta_keywords?: string[];
  meta_description?: string;
  search_keywords?: string;
  image_url?: string;
  custom_url?: {
    url: string;
    is_customized: boolean;
  };
}

// Promotions & Gift Certificates
export interface Promotion {
  id: number;
  redemption_type: string;
  name: string;
  customer?: {
    group_ids: number[];
    minimum_order_count: number;
    excluded_group_ids: number[];
    segments?: {
      ids: number[];
    };
  };
  rules?: Array<{
    action: string;
    apply_once: boolean;
    stop: boolean;
    add_free_item?: boolean;
    as_total?: boolean;
    include_items_considered_by_condition?: boolean;
    item_criteria?: {
      brands?: {
        mode: string;
        ids: number[];
      };
      products?: {
        mode: string;
        ids: number[];
      };
      categories?: {
        mode: string;
        ids: number[];
      };
    };
  }>;
  notifications?: Array<{
    type: string;
    content: string;
    locations: string[];
  }>;
  stop: boolean;
  currency_code: string;
  current_uses: number;
  max_uses?: number;
  start_date: string;
  end_date?: string;
  status: 'enabled' | 'disabled' | 'invalid';
  schedule?: {
    daily_start_time?: string;
    daily_end_time?: string;
    weekly?: {
      sunday: boolean;
      monday: boolean;
      tuesday: boolean;
      wednesday: boolean;
      thursday: boolean;
      friday: boolean;
      saturday: boolean;
    };
  };
  can_be_used_with_other_promotions: boolean;
}

export interface GiftCertificate {
  id: number;
  code: string;
  original_balance: string;
  starting_balance: string;
  remaining_balance: string;
  to_name: string;
  to_email: string;
  from_name: string;
  from_email: string;
  created_date: string;
  expiry_date?: string;
  template: string;
  message?: string;
  code_type: string;
  status: 'active' | 'pending' | 'disabled' | 'expired';
  order_id?: number;
  currency_code: string;
}

// Store, Channel & Site Types
export interface StoreInformation {
  id: string;
  domain: string;
  secure_url: string;
  name: string;
  first_name: string;
  last_name: string;
  address: string;
  country: string;
  country_code: string;
  phone: string;
  admin_email: string;
  order_email: string;
  favicon_url: string;
  timezone: {
    name: string;
    raw_offset: number;
    dst_offset: number;
    dst_correction: boolean;
    date_format: {
      display: string;
      export: string;
      extended_display: string;
    };
  };
  language: string;
  currency: string;
  currency_symbol: string;
  decimal_separator: string;
  thousands_separator: string;
  decimal_places: number;
  currency_symbol_location: string;
  weight_units: string;
  dimension_units: string;
  dimension_decimal_places: number;
  dimension_decimal_token: string;
  dimension_thousands_token: string;
  plan_name: string;
  plan_level: string;
  plan_is_trial: boolean;
  industry: string;
  logo: {
    title: string;
    url: string;
  };
  is_price_entered_with_tax: boolean;
  active_comparison_modules: string[];
  features: {
    stencil_enabled: boolean;
    sitewidehttps_enabled: boolean;
    facebook_catalog_id: string;
    checkout_type: string;
  };
}

export interface Channel {
  id: number;
  name: string;
  type: string;
  platform: string;
  status: 'active' | 'inactive' | 'archived' | 'deleted' | 'terminated';
  is_listable_from_ui: boolean;
  is_visible: boolean;
  date_created: string;
  date_modified: string;
  external_id?: string;
  is_enabled: boolean;
}

export interface Site {
  id: number;
  url: string;
  channel_id: number;
  created_at: string;
  updated_at: string;
  routes: Array<{
    id: number;
    type: string;
    matching: string;
    route: string;
  }>;
}

// Webhook Types
export interface Webhook {
  id: number;
  client_id: string;
  store_hash: string;
  created_at: number;
  updated_at: number;
  scope: string;
  destination: string;
  is_active: boolean;
  headers?: Record<string, string>;
}

// Script & Page Types
export interface Script {
  uuid: string;
  name: string;
  description?: string;
  html: string;
  src?: string;
  auto_uninstall: boolean;
  load_method: 'default' | 'async' | 'defer';
  location: 'head' | 'footer';
  visibility: 'all_pages' | 'storefront' | 'checkout' | 'order_confirmation';
  kind: 'src' | 'script_tag';
  api_client_id: string;
  date_created: string;
  date_modified: string;
  consent_category: 'essential' | 'functional' | 'analytics' | 'targeting';
  enabled: boolean;
}

export interface Page {
  id: number;
  channel_id: number;
  name: string;
  is_visible: boolean;
  parent_id: number;
  sort_order: number;
  type: 'page' | 'raw' | 'contact_form' | 'feed' | 'link' | 'blog';
  is_homepage: boolean;
  is_customers_only: boolean;
  email?: string;
  meta_title?: string;
  body?: string;
  feed?: string;
  link?: string;
  contact_fields?: string;
  meta_keywords?: string;
  meta_description?: string;
  search_keywords?: string;
  url?: string;
}

// Shipping Types
export interface ShippingZone {
  id: number;
  name: string;
  type: 'global' | 'country' | 'state' | 'zip';
  locations: Array<{
    id: number;
    zip_start?: string;
    zip_end?: string;
    country_iso2?: string;
    state_iso2?: string;
    enabled: boolean;
  }>;
  free_shipping?: {
    enabled: boolean;
    minimum_sub_total: string;
    exclude_fixed_shipping_products: boolean;
  };
  handling_rates?: {
    display_separately: boolean;
    fixed_surcharge: string;
  };
  enabled: boolean;
}

export interface ShippingMethod {
  id: number;
  name: string;
  type: 'perorder' | 'peritem' | 'weight' | 'total' | 'auspost' | 'canadapost' | 'endicia' | 'usps' | 'fedex' | 'ups' | 'upsready' | 'freightquote';
  settings?: {
    rate: number;
  };
  enabled: boolean;
  handling_fees?: {
    fixed_surcharge: number;
    display_separately: boolean;
  };
  is_fallback: boolean;
}

// Tax Types
export interface TaxClass {
  id: number;
  name: string;
}

// Payment Method Types
export interface PaymentMethod {
  id: string;
  name: string;
  test_mode: boolean;
  type: string;
  supported_instruments: Array<{
    instrument_type: string;
  }>;
  supported_payment_methods?: string[];
  stored_instruments?: Array<{
    type: string;
  }>;
}

// Currency Types
export interface Currency {
  id: number;
  is_default: boolean;
  last_updated: string;
  country_iso2: string;
  currency_code: string;
  currency_exchange_rate: string;
  auto_update: boolean;
  location_code?: string;
  token_location: 'left' | 'right';
  token: string;
  decimal_token: string;
  thousands_token: string;
  decimal_places: number;
  enabled: boolean;
  is_transactional: boolean;
  name: string;
}

// Wishlist Types
export interface Wishlist {
  id: number;
  customer_id: number;
  name: string;
  is_public: boolean;
  token: string;
  items: Array<{
    id: number;
    product_id: number;
    variant_id: number;
  }>;
}
