// =============================================================================
// Feed Management System — Core Types
// =============================================================================

/** Raw key-value product data from a provider */
export interface RawProduct {
  [key: string]: string | number | boolean | null | undefined;
}

/** Product after field mapping + normalization */
export interface NormalizedProduct {
  name: string;
  brand: string;
  model?: string;
  colorway?: string;
  ean?: string;
  sku?: string;
  price: number;
  original_price?: number;
  currency: string;
  product_url: string;
  image_url?: string;
  description?: string;
  category?: string;
  size?: string;
  in_stock: boolean;
  // Normalized versions for matching
  brand_normalized: string;
  model_normalized?: string;
  colorway_normalized?: string;
  match_fingerprint?: string;
  // Original external ID from feed
  external_id?: string;
}

/** A feed listed from an external provider */
export interface ExternalFeed {
  externalId: string;
  name: string;
  merchantName: string;
  productCount: number;
  lastUpdated: Date | null;
  feedUrl?: string;
}

export interface FeedInfo {
  productCount: number;
  lastUpdated: Date | null;
  categories?: string[];
}

export interface FetchOptions {
  offset?: number;
  limit?: number;
  modifiedSince?: Date;
}

export interface ProviderConfig {
  [key: string]: any;
}

/** Provider adapter interface */
export interface FeedProvider {
  readonly slug: string;
  authenticate(config: ProviderConfig): Promise<void>;
  listFeeds(): Promise<ExternalFeed[]>;
  fetchProducts(feedId: string, options?: FetchOptions): AsyncGenerator<RawProduct[]>;
  getFeedInfo(feedId: string): Promise<FeedInfo>;
}

/** DB row types */
export interface ProviderRow {
  id: number;
  slug: string;
  name: string;
  adapter_type: string;
  config: ProviderConfig;
  is_active: boolean;
}

export interface FeedRow {
  id: number;
  provider_id: number;
  external_id: string | null;
  name: string;
  merchant_name: string | null;
  feed_url: string | null;
  sync_frequency: string;
  is_enabled: boolean;
  priority: number;
  config: any;
  product_count: number;
  last_sync_at: Date | null;
  last_sync_status: string | null;
  last_error: string | null;
  created_at: Date;
  updated_at: Date;
  // Joined fields
  provider_slug?: string;
  provider_config?: ProviderConfig;
}

export interface FieldMappingRow {
  id: number;
  provider_id: number | null;
  feed_id: number | null;
  source_field: string;
  target_field: string;
  transform: string | null;
  transform_args: any;
  is_active: boolean;
}

export interface SyncLogRow {
  id: number;
  feed_id: number;
  started_at: Date;
  finished_at: Date | null;
  status: string;
  products_fetched: number;
  products_new: number;
  products_updated: number;
  products_removed: number;
  products_matched: number;
  errors_count: number;
  error_details: any;
  metadata: any;
}

export interface SyncStats {
  fetched: number;
  new: number;
  updated: number;
  removed: number;
  matched: number;
  errors: number;
  errorDetails: Array<{ row?: number; field?: string; message: string }>;
}

export interface MatchResult {
  productId: number | null;
  confidence: number;
  matchType: 'ean' | 'exact_model' | 'fuzzy' | 'new';
}
