from typing import Dict, List, Any class ColumnMapper: """ Maps dynamic source column names (e.g., 'Item Code', 'Selling Price') to standard system target fields (e.g., 'sku', 'price'). Provides heuristic / AI auto-mapping suggestions. """ STANDARD_FIELDS = { "sku": ["sku", "item_code", "item code", "product_code", "part_number", "model_number"], "name": ["product_name", "name", "title", "product name", "item_name"], "parent_name": ["parent_name", "base_product", "product_group", "parent_product", "model_series", "parent_title"], "price": ["price", "selling_price", "mrp", "unit_price", "rate"], "cost_price": ["cost_price", "purchase_price", "buying_price", "cost price"], "stock": ["stock", "quantity", "qty", "inventory", "stock_count", "balance"], "brand": ["brand", "brand_name", "manufacturer", "make"], "category": ["category", "category_name", "department", "group"], "parent_category": ["parent_category", "parent_category_name", "parent category"], "is_parent_feature": ["is_parent_feature", "parent_feature", "is parent feature", "featured_category"], "parent_media_key": ["parent_media_key", "parent_image_key", "parent media key"], "media_key": ["media_key", "media key", "image_key", "folder_key", "media_group", "variant_media_key"], "barcode": ["barcode", "upc", "ean", "isbn"], "description": ["description", "details", "specifications", "summary", "product_description"], "seo_title": ["seo_title", "seo title", "meta_title", "page_title", "seo_name"], "seo_description": ["seo_description", "seo description", "meta_description", "seo_desc"], "meta_keywords": ["meta_keywords", "meta keywords", "seo_keywords", "keywords"], "device_series": ["device_series", "series", "device series", "series_name"], "device_model": ["device_model", "model", "device model", "target_model", "compatibility_model"], "device_type": ["device_type", "device type", "type", "device_category"], "warranty_type": ["warranty_type", "warranty type", "warranty_mode", "guarantee_type"], "warranty_summary": ["warranty_summary", "warranty summary", "warranty_details", "warranty_desc", "warranty_info", "guarantee_details"], "show_specifications": ["show_specifications", "show specifications", "show_specification", "specifications_enabled", "show_specs", "specifications_tab"], "is_service_booking": ["is_service_booking", "enable_service_booking_mapping", "service_booking_enabled", "service_booking", "service_mapping", "service_booking_mapping"], "service_category": ["service_category", "service_type", "mapped_service_category", "mapped_service_category_id", "service_category_name", "repair_service_category"], "store_locations": ["store_locations", "locations", "branch_inventory", "location_mappings", "branch_locations", "store_location", "branches"] } @classmethod def suggest_mappings(cls, headers: List[str]) -> Dict[str, str]: """ Suggests mapping of source header -> standard target field. """ suggestions = {} for header in headers: normalized = header.lower().strip().replace("-", "_") matched = False for target_field, aliases in cls.STANDARD_FIELDS.items(): if normalized == target_field or normalized in aliases: suggestions[header] = target_field matched = True break if not matched: # Fuzzy keyword match fallback for target_field, aliases in cls.STANDARD_FIELDS.items(): for alias in aliases: if alias in normalized: # Avoid matching 'rate' in non-price contexts like 'refresh_rate' or 'heart_rate' if alias == 'rate' and ('refresh' in normalized or 'heart' in normalized): continue suggestions[header] = target_field matched = True break if matched: break if not matched: suggestions[header] = header # Keep as custom dynamic attribute return suggestions @classmethod def apply_mapping(cls, raw_row: Dict[str, Any], column_maps: Dict[str, str]) -> Dict[str, Any]: """ Transforms a raw row using the given column_maps dictionary. Auto-generates mapping suggestions if column_maps is empty or missing. """ if not column_maps or column_maps == {}: column_maps = cls.suggest_mappings(list(raw_row.keys())) mapped_row = {} attributes = {} for source_col, val in raw_row.items(): target_field = column_maps.get(source_col, source_col) if target_field in cls.STANDARD_FIELDS: mapped_row[target_field] = val else: attributes[target_field] = val if attributes: mapped_row["attributes"] = attributes if not mapped_row.get("sku") or not str(mapped_row.get("sku")).strip(): mapped_row["sku"] = cls.generate_sku(mapped_row) return mapped_row @classmethod def generate_sku(cls, mapped_row: Dict[str, Any], suffix: str = "") -> str: """ Automatically generates a clean uppercase SKU from name, device_type, and device_model. Format: [BRAND]-[MODEL_SLUG]-[NAME_SLUG] """ import re device_type = str(mapped_row.get("device_type") or (mapped_row.get("attributes") or {}).get("device_type") or "").strip().upper() device_model = str(mapped_row.get("device_model") or (mapped_row.get("attributes") or {}).get("device_model") or "").strip() brand = str(mapped_row.get("brand") or (mapped_row.get("attributes") or {}).get("brand") or "").strip() name = str(mapped_row.get("name") or mapped_row.get("parent_name") or "ITEM").strip() def clean_slug(text: str, max_words: int = 5) -> str: text = re.sub(r'[^a-zA-Z0-9\s]+', ' ', text) words = [w.upper() for w in text.split() if w] return "-".join(words[:max_words]) parts = [] if brand: parts.append(clean_slug(brand, 1)) if device_model: model_slug = clean_slug(device_model, 4) if parts and model_slug.startswith(parts[0] + "-"): model_slug = model_slug[len(parts[0]) + 1:] parts.append(model_slug) elif device_type: parts.append(clean_slug(device_type, 1)) name_slug = clean_slug(name, 4) if name_slug: parts.append(name_slug) sku_base = "-".join([p for p in parts if p]).strip("-") if not sku_base: sku_base = "IFX-ITEM" if suffix: sku_base = f"{sku_base}-{suffix}" return sku_base