export interface Market {
  d: string
  pubkey: string
  relays: string[]
  selected: boolean
  opts: {
    name: string
    description?: string
    logo?: string
    banner?: string
    merchants: string[]
    ui?: Record<string, any>
  }
}

// Domain Model - Single source of truth for Stall
export interface Stall {
  id: string
  pubkey: string
  name: string
  description?: string
  logo?: string
  categories?: string[]
  shipping: ShippingZone[]
  currency: string
  nostrEventId?: string
}

// Domain Model - Single source of truth for Product
export interface Product {
  id: string
  stall_id: string
  stallName: string
  name: string
  description?: string
  price: number
  currency: string
  quantity: number
  images?: string[]
  categories?: string[]
  createdAt: number
  updatedAt: number
  nostrEventId?: string
  // API-specific properties for merchant store management
  active?: boolean
  pending?: boolean
  config?: { currency?: string, [key: string]: any }
}

// Type aliases for API responses - imported from services
export type { ProductApiResponse } from '../services/nostrmarketAPI'

// Mapping function to convert API response to domain model
export function mapApiResponseToProduct(
  apiProduct: import('../services/nostrmarketAPI').ProductApiResponse,
  stallName: string,
  stallCurrency: string = 'sats'
): Product {
  return {
    id: apiProduct.id || `${apiProduct.stall_id}-${Date.now()}`,
    stall_id: apiProduct.stall_id,
    stallName,
    name: apiProduct.name,
    description: apiProduct.config?.description || '',
    price: apiProduct.price,
    currency: stallCurrency,
    quantity: apiProduct.quantity,
    images: apiProduct.images,
    categories: apiProduct.categories,
    createdAt: apiProduct.event_created_at || Date.now(),
    updatedAt: Date.now(),
    nostrEventId: apiProduct.event_id,
    active: apiProduct.active,
    pending: apiProduct.pending,
    config: apiProduct.config
  }
}

// Mapper function to convert API response to domain model
export function mapApiResponseToStall(
  apiStall: StallApiResponse,
  pubkey: string = '',
  categories: string[] = []
): Stall {
  return {
    id: apiStall.id,
    pubkey,
    name: apiStall.name,
    description: apiStall.config?.description,
    logo: apiStall.config?.image_url,
    categories,
    shipping: apiStall.shipping_zones?.map(zone => ({
      id: zone.id,
      name: zone.name,
      cost: zone.cost,
      currency: apiStall.currency,
      countries: zone.countries,
      description: `${zone.name} shipping`,
      estimatedDays: undefined,
      requiresPhysicalShipping: true
    })) || [],
    currency: apiStall.currency,
    nostrEventId: apiStall.event_id
  }
}

export interface Order {
  id: string
  cartId: string
  stallId: string
  buyerPubkey: string
  sellerPubkey: string
  status: OrderStatus
  items: OrderItem[]
  contactInfo: ContactInfo
  shippingZone: ShippingZone
  paymentRequest?: string
  paymentMethod: PaymentMethod
  subtotal: number
  shippingCost: number
  total: number
  currency: string
  createdAt: number
  updatedAt: number
  nostrEventId?: string
  nostrEventSig?: string
  sentViaNostr?: boolean
  nostrError?: string
  originalOrderId?: string
  lightningInvoice?: any
  paymentHash?: string
  paidAt?: number
  paymentStatus?: 'pending' | 'paid' | 'expired'
  paid?: boolean  // Direct boolean field matching nostrmarket reference
  shipped?: boolean  // Shipping status from merchant updates
  qrCodeDataUrl?: string
  qrCodeLoading?: boolean
  qrCodeError?: string | null
  showQRCode?: boolean
}

export interface OrderItem {
  productId: string
  productName: string
  quantity: number
  price: number
  currency: string
}

export interface ContactInfo {
  address?: string
  email?: string
  message?: string
  npub?: string
}

export interface ShippingZone {
  id: string
  name: string
  cost: number
  currency: string
  countries?: string[]  // Countries/regions this zone covers
  description?: string
  estimatedDays?: string
  requiresPhysicalShipping?: boolean
}

// API Response Types - Raw data from LNbits backend
export interface StallApiResponse {
  id: string
  wallet: string
  name: string
  currency: string
  shipping_zones: Array<{
    id: string
    name: string
    cost: number
    countries: string[]
  }>
  config: {
    image_url?: string
    description?: string
  }
  pending: boolean
  event_id?: string
  event_created_at?: number
}

export type OrderStatus = 'pending' | 'paid' | 'shipped' | 'delivered' | 'cancelled' | 'processing'

export type PaymentMethod = 'lightning' | 'btc_onchain'

export interface CartItem {
  product: Product
  quantity: number
  stallId: string
}

export interface StallCart {
  id: string
  merchant: string
  products: CartItem[]
  subtotal: number
  shippingZone?: ShippingZone
  currency: string
}

export interface FilterData {
  categories: string[]
  merchants: string[]
  stalls: string[]
  currency: string | null
  priceFrom: number | null
  priceTo: number | null
  inStock: boolean | null
  paymentMethods: PaymentMethod[]
}

export interface SortOptions {
  field: string
  order: 'asc' | 'desc'
}

export interface PaymentRequest {
  paymentRequest: string
  amount: number
  currency: string
  expiresAt: number
  description: string
}

export interface PaymentStatus {
  paid: boolean
  amountPaid: number
  paidAt?: number
  transactionId?: string
}