/**
 * PlentyONE API Type Definitions
 *
 * These types are based on the PlentyONE REST API documentation.
 * @see https://developers.plentymarkets.com/
 */

// ============================================================================
// Authentication Types
// ============================================================================

export interface PlentyoneAuthConfig {
  /** PlentyONE system URL (e.g., https://yourshop.plentymarkets-cloud01.com) */
  baseUrl: string;
  /** OAuth2 Client ID */
  clientId: string;
  /** OAuth2 Client Secret */
  clientSecret: string;
}

export interface PlentyoneTokenResponse {
  access_token: string;
  token_type: string;
  expires_in: number;
  refresh_token?: string;
  scope?: string;
}

export interface PlentyoneTokenInfo {
  accessToken: string;
  expiresAt: Date;
  refreshToken?: string;
}

// ============================================================================
// Order Types
// ============================================================================

export interface PlentyoneOrder {
  id: number;
  typeId: number;
  statusId: number;
  statusName?: string;
  plentyId: number;
  locationId: number;
  createdAt: string;
  updatedAt: string;
  lockStatus: string;
  ownerId?: number;
  referrerId: number;
  amounts?: PlentyoneOrderAmount[];
  orderItems?: PlentyoneOrderItem[];
  properties?: PlentyoneOrderProperty[];
  addresses?: PlentyoneAddress[];
  relations?: PlentyoneOrderRelation[];
}

export interface PlentyoneOrderAmount {
  id: number;
  orderId: number;
  isSystemCurrency: boolean;
  isNet: boolean;
  currency: string;
  exchangeRate: number;
  netTotal: number;
  grossTotal: number;
  vatTotal: number;
  invoiceTotal: number;
  paidAmount: number;
  prepaidAmount: number;
  giftCardAmount: number;
  shippingCostsGross: number;
  shippingCostsNet: number;
  createdAt: string;
  updatedAt: string;
}

export interface PlentyoneOrderItem {
  id: number;
  orderId: number;
  typeId: number;
  referrerId: number;
  itemVariationId: number;
  quantity: number;
  orderItemName: string;
  attributeValues?: string;
  shippingProfileId: number;
  amounts?: PlentyoneOrderItemAmount[];
  properties?: PlentyoneOrderItemProperty[];
  createdAt: string;
  updatedAt: string;
}

export interface PlentyoneOrderItemAmount {
  id: number;
  orderItemId: number;
  isSystemCurrency: boolean;
  currency: string;
  exchangeRate: number;
  purchasePrice: number;
  priceOriginalGross: number;
  priceOriginalNet: number;
  priceGross: number;
  priceNet: number;
  surcharge: number;
  discount: number;
  isPercentage: boolean;
  createdAt: string;
  updatedAt: string;
}

export interface PlentyoneOrderItemProperty {
  id: number;
  orderItemId: number;
  typeId: number;
  value: string;
  createdAt: string;
  updatedAt: string;
}

export interface PlentyoneOrderProperty {
  id: number;
  orderId: number;
  typeId: number;
  value: string;
  createdAt: string;
  updatedAt: string;
}

export interface PlentyoneAddress {
  id: number;
  gender: string;
  name1?: string;
  name2: string;
  name3: string;
  name4?: string;
  address1: string;
  address2: string;
  address3?: string;
  address4?: string;
  postalCode: string;
  town: string;
  countryId: number;
  stateId?: number;
  phone?: string;
  email?: string;
  isPackstation: boolean;
  isPostfiliale: boolean;
  createdAt: string;
  updatedAt: string;
}

export interface PlentyoneOrderRelation {
  orderId: number;
  referenceType: string;
  referenceId: number;
  relation: string;
}

// ============================================================================
// Order Query Types
// ============================================================================

export interface PlentyoneOrdersQuery {
  /** Order ID to search for */
  orderId?: number;
  /** Multiple order IDs separated by comma */
  orderIds?: string;
  /** Filter by order type (1=Sales, 2=Delivery, etc.) */
  orderType?: number;
  /** Filter by referrer ID */
  referrerId?: number;
  /** Filter by status ID */
  statusId?: number;
  /** Filter by status range (from) */
  statusIdFrom?: number;
  /** Filter by status range (to) */
  statusIdTo?: number;
  /** Filter by external order ID */
  externalOrderId?: string;
  /** Filter by creation date (from) */
  createdAtFrom?: string;
  /** Filter by creation date (to) */
  createdAtTo?: string;
  /** Filter by update date (from) */
  updatedAtFrom?: string;
  /** Filter by update date (to) */
  updatedAtTo?: string;
  /** Page number */
  page?: number;
  /** Items per page (max 250) */
  itemsPerPage?: number;
  /** Relations to include (amounts, orderItems, properties, addresses) */
  with?: string;
}

export interface PlentyonePaginatedResponse<T> {
  page: number;
  totalsCount: number;
  isLastPage: boolean;
  lastPageNumber: number;
  firstOnPage: number;
  lastOnPage: number;
  itemsPerPage: number;
  entries: T[];
}

// ============================================================================
// Stock Types
// ============================================================================

export interface PlentyoneStockItem {
  itemId: number;
  variationId: number;
  warehouseId: number;
  stockPhysical: number;
  reservedStock: number;
  reservedEbay: number;
  reservedBundle: number;
  reorderDelta: number;
  stockNet: number;
  warehouseName?: string;
  warehouseType?: string;
  averagePurchasePrice?: number;
  updatedAt: string;
}

export interface PlentyoneStockQuery {
  /** Filter by variation ID */
  variationId?: number;
  /** Filter by item ID */
  itemId?: number;
  /** Filter by warehouse ID */
  warehouseId?: number;
  /** Filter by updated date (from) */
  updatedAtFrom?: string;
  /** Filter by updated date (to) */
  updatedAtTo?: string;
  /** Page number */
  page?: number;
  /** Items per page */
  itemsPerPage?: number;
}

export interface PlentyoneStockMovement {
  id: number;
  itemId: number;
  variationId: number;
  warehouseId: number;
  reasonId: number;
  attributeValues: string;
  processRowId: number;
  quantity: number;
  reasonString?: string;
  createdAt: string;
  updatedAt: string;
}

// ============================================================================
// Statistics Types
// ============================================================================

export interface PlentyoneRevenueStats {
  date: string;
  ordersCount: number;
  itemsCount: number;
  revenue: number;
  revenueNet: number;
  currency: string;
}

export interface PlentyoneStatsQuery {
  /** Start date (YYYY-MM-DD) */
  dateFrom: string;
  /** End date (YYYY-MM-DD) */
  dateTo: string;
  /** Group by (day, week, month) */
  groupBy?: 'day' | 'week' | 'month';
  /** Filter by referrer ID */
  referrerId?: number;
  /** Filter by status ID */
  statusId?: number;
}

export interface PlentyoneOrderStats {
  totalOrders: number;
  totalRevenue: number;
  totalRevenueNet: number;
  averageOrderValue: number;
  currency: string;
  ordersByStatus: Record<number, number>;
  ordersByReferrer: Record<number, number>;
}

// ============================================================================
// Warehouse Types
// ============================================================================

export interface PlentyoneWarehouse {
  id: number;
  name: string;
  typeId: number;
  note?: string;
  onStockAvailability: number;
  outOfStockAvailability: number;
  splitByShippingProfile: boolean;
  isInventoryModeActive: boolean;
  logisticsType: string;
  updatedAt: string;
  createdAt: string;
}

// ============================================================================
// Item Types
// ============================================================================

export interface PlentyoneItem {
  id: number;
  position: number;
  manufacturerId?: number;
  producingCountryId?: number;
  ageRestriction: number;
  conditionId: number;
  isNet: boolean;
  customsTariffNumber?: string;
  flagOne: number;
  flagTwo: number;
  isSubscribable: boolean;
  isSerialNumberRequired: boolean;
  isShippingPackage: boolean;
  createdAt: string;
  updatedAt: string;
  variations?: PlentyoneVariation[];
}

export interface PlentyoneVariation {
  id: number;
  itemId: number;
  mainVariationId?: number;
  position: number;
  isMain: boolean;
  isActive: boolean;
  number: string;
  model?: string;
  name?: string;
  purchasePrice: number;
  movingAveragePrice: number;
  priceCalculationUUID?: string;
  weightG: number;
  weightNetG: number;
  widthMM: number;
  lengthMM: number;
  heightMM: number;
  unitsContained: number;
  palletTypeId?: number;
  packingUnits: number;
  packingUnitTypeId?: number;
  customsPercent: number;
  vatId: number;
  availableUntil?: string;
  createdAt: string;
  updatedAt: string;
}

// ============================================================================
// Webhook Types
// ============================================================================

export interface PlentyoneWebhook {
  id: number;
  event: string;
  url: string;
  isActive: boolean;
  createdAt: string;
  updatedAt: string;
}

export interface PlentyoneWebhookPayload {
  event: string;
  timestamp: string;
  data: Record<string, unknown>;
}

// ============================================================================
// API Response Types
// ============================================================================

export interface PlentyoneApiError {
  error: string;
  error_description?: string;
  message?: string;
  validation?: Record<string, string[]>;
}
