// ==============================================================================
// CATALOG & FEED MODULE TYPES
// ==============================================================================

export type SyncScope = 'all' | 'selected';

export type VariantExportMode =
  | 'all_separate'        // 1. Export every variant as a separate item
  | 'grouped'             // 2. Export variants as grouped items (item_group_id)
  | 'first_variant_title' // 3. Export only the first variant (variant title and variant id)
  | 'first_product_title';// 4. Export only the first variant (product title and product id)

export type LinkDomainMode = 'myshopify' | 'primary' | 'custom';

export type PriceMode =
  | 'compare_at_and_price' // Use both "Compare at" price and price (default)
  | 'price_only'           // Use only the Shopify price
  | 'compare_at_only';     // Use "Compare at" price from Shopify

export type ScheduleType = 'daily' | 'weekly';

export type CatalogStatus =
  | 'synced'
  | 'syncing'
  | 'failed'
  | 'pending'
  | 'needs_reconnect';

export interface CatalogRecord {
  id: string;
  shop_id: string;
  facebook_account_id?: string | null;
  fb_catalog_id: string;
  fb_business_id: string;
  fb_pixel_id?: string | null;
  name: string;
  sync_scope: SyncScope;
  sync_rules?: Record<string, any> | null;
  variant_mode: VariantExportMode;
  google_category_id?: string | null;
  google_category_name?: string | null;
  additional_images: boolean;
  link_domain: LinkDomainMode;
  custom_domain?: string | null;
  exclude_out_of_stock?: boolean;
  price_mode: PriceMode;
  schedule_type: ScheduleType;
  schedule_day: string; // e.g. 'Every Monday', 'Every Day'
  schedule_hour: number; // 01 - 12
  schedule_period: 'AM' | 'PM';
  schedule_tz: string; // e.g. 'America/New_York', 'UTC'
  auto_sync: boolean;
  pixel_tracking: boolean;
  status: CatalogStatus;
  last_synced_at?: string | null;
  next_sync_at?: string | null;
  last_error?: string | null;
  fb_catalog_url?: string | null;
  product_count: number;
  variant_count: number;
  created_at: string;
  updated_at: string;
}

export type SelectionType = 'collection' | 'tag' | 'product';

export interface CatalogSelectionRecord {
  id: string;
  catalog_id: string;
  type: SelectionType;
  shopify_gid: string; // e.g. "gid://shopify/Product/123" or tag name
  value: string;       // Display title / label / tag name
  is_exclude: boolean;
}

export interface CatalogItemRecord {
  id: string;
  catalog_id: string;
  shopify_product_id: string;
  shopify_variant_id: string;
  retailer_id: string;
  payload_hash: string;
  fb_status?: string | null;
  last_error?: string | null;
  synced_at: string;
}

export type SyncTrigger = 'create' | 'manual' | 'schedule' | 'webhook' | 'edit';

export interface CatalogSyncLogRecord {
  id: string;
  catalog_id: string;
  trigger: SyncTrigger;
  status: 'running' | 'completed' | 'failed';
  created_count: number;
  updated_count: number;
  deleted_count: number;
  failed_count: number;
  batch_handles?: string[] | null;
  error_details?: string | null;
  started_at: string;
  finished_at?: string | null;
}

export interface CatalogFormData {
  business_id: string;
  fb_catalog_id?: string;
  pixel_id?: string | null;
  name: string;
  sync_scope: SyncScope;
  selected_collections: Array<{ id: string; title: string }>;
  selected_tags: string[];
  selected_products: Array<{ id: string; title: string; image?: string; variantsCount?: number }>;
  exclude_products_active: boolean;
  exclude_out_of_stock?: boolean;
  excluded_collections: Array<{ id: string; title: string }>;
  excluded_tags: string[];
  excluded_products: Array<{ id: string; title: string; image?: string; variantsCount?: number }>;
  variant_mode: VariantExportMode;
  google_category_id: string;
  google_category_name: string;
  additional_images: boolean;
  link_domain: LinkDomainMode;
  custom_domain?: string;
  price_mode: PriceMode;
  schedule_type: ScheduleType;
  schedule_day: string;
  schedule_hour: number;
  schedule_period: 'AM' | 'PM';
  schedule_tz: string;
  auto_sync?: boolean;
  pixel_tracking?: boolean;
}

export interface GoogleCategory {
  id: string;
  name: string;
  full_path: string;
  path?: string;
}

export interface MetaBatchItemPayload {
  id: string;
  title: string;
  description?: string;
  rich_text_description?: string;
  product_type?: string;
  mobile_link?: string;
  mpn?: string;
  gtin?: string;
  shipping_weight?: string;
  availability?: string;
  condition?: string;
  price?: string;
  sale_price?: string;
  link?: string;
  image_link?: string;
  image_url?: string;
  additional_image_link?: string[];
  additional_image_urls?: string[];
  additional_image_cdn_urls?: string[];
  video?: string | Array<{ url: string; tag?: string }>;
  video_url?: string;
  videos?: Array<{ url: string; tags?: string[] }>;
  brand?: string;
  google_product_category?: string;
  fb_product_category?: string;
  item_group_id?: string;
  custom_label_0?: string;
  custom_label_1?: string;
  [key: string]: any;
}

export interface MetaBatchItemRequest {
  method: 'CREATE' | 'UPDATE' | 'DELETE';
  retailer_id: string;
  data?: MetaBatchItemPayload | Record<string, unknown>;
}
