#!/usr/bin/env python3
"""
Search Index Generator for Hugo Static Site
Processes CSV files and generates letter-based JSON search indexes
"""

import csv
import json
import os
import re
from pathlib import Path
from collections import defaultdict

def normalize_mpn(mpn):
    """Normalize MPN by converting to uppercase and removing spaces"""
    if not mpn:
        return ""
    return re.sub(r'\s+', '', mpn.upper())

def extract_sku_data(row):
    """Extract relevant SKU data from CSV row"""
    mpn = row.get('mpn', '').strip()
    brand = row.get('brand', '').strip()
    title = row.get('title', '').strip()
    category = row.get('category', '').strip()
    stock = int(row.get('stock', '0'))
    price = row.get('price', '').strip()
    description = row.get('description', '').strip()
    
    # Generate URL based on MPN (can be customized)
    url = f"/items/{normalize_mpn(mpn)}/"
    
    return {
        'mpn': mpn,
        'brand': brand,
        'title': title,
        'category': category,
        'stock': stock,
        'price': price,
        'description': description,
        'url': url,
        'normalized_mpn': normalize_mpn(mpn)
    }

def process_csv_file(file_path):
    """Process a single CSV file and return SKU data"""
    skus = []
    try:
        with open(file_path, 'r', encoding='utf-8') as file:
            reader = csv.DictReader(file)
            for row in reader:
                try:
                    sku_data = extract_sku_data(row)
                    if sku_data['mpn']:  # Only include rows with MPN
                        skus.append(sku_data)
                except Exception as e:
                    print(f"Warning: Skipping row in {file_path}: {e}")
                    continue
    except Exception as e:
        print(f"Error processing {file_path}: {e}")
    return skus

def group_by_first_letter(skus):
    """Group SKUs by the first letter of their normalized MPN"""
    grouped = defaultdict(list)
    
    for sku in skus:
        normalized_mpn = sku['normalized_mpn']
        if normalized_mpn:
            first_letter = normalized_mpn[0]
            # Only process alphanumeric characters
            if first_letter.isalnum():
                grouped[first_letter].append(sku)
    
    return grouped

def generate_search_indexes(data_src_dir, output_dir):
    """Main function to generate search indexes"""
    
    # Ensure output directory exists
    os.makedirs(output_dir, exist_ok=True)
    
    # Collect all SKUs from CSV files
    all_skus = []
    
    print(f"Processing CSV files from {data_src_dir}...")
    
    # Process all CSV files in data-src directory
    csv_files = list(Path(data_src_dir).glob("*.csv"))
    
    if not csv_files:
        print(f"No CSV files found in {data_src_dir}")
        return
    
    for csv_file in csv_files:
        print(f"Processing {csv_file.name}...")
        skus = process_csv_file(csv_file)
        all_skus.extend(skus)
        print(f"  - Found {len(skus)} SKUs")
    
    print(f"Total SKUs found: {len(all_skus)}")
    
    # Group by first letter
    grouped_skus = group_by_first_letter(all_skus)
    
    # Generate JSON files for each letter
    print(f"Generating search indexes in {output_dir}...")
    
    for letter, skus in grouped_skus.items():
        output_file = os.path.join(output_dir, f"{letter.upper()}.json")
        
        # Sort SKUs by MPN for better search experience
        skus_sorted = sorted(skus, key=lambda x: x['normalized_mpn'])
        
        # Create index data
        index_data = {
            'letter': letter.upper(),
            'count': len(skus_sorted),
            'skus': skus_sorted,
            'generated_at': os.path.getmtime(__file__)
        }
        
        try:
            with open(output_file, 'w', encoding='utf-8') as f:
                json.dump(index_data, f, ensure_ascii=False, indent=2)
            print(f"  - Generated {output_file} ({len(skus_sorted)} SKUs)")
        except Exception as e:
            print(f"Error writing {output_file}: {e}")
    
    # Generate a summary file
    summary = {
        'total_skus': len(all_skus),
        'letters': sorted(grouped_skus.keys()),
        'files_generated': len(grouped_skus),
        'generated_at': os.path.getmtime(__file__)
    }
    
    summary_file = os.path.join(output_dir, "_summary.json")
    try:
        with open(summary_file, 'w', encoding='utf-8') as f:
            json.dump(summary, f, ensure_ascii=False, indent=2)
        print(f"  - Generated summary: {summary_file}")
    except Exception as e:
        print(f"Error writing summary file: {e}")

def main():
    """Main entry point"""
    # Get the current directory (where the script is located)
    hugo_root = os.path.dirname(os.path.abspath(__file__))
    
    # Set up paths - use correct relative paths
    data_src_dir = os.path.join(hugo_root, "data-src")
    output_dir = os.path.join(hugo_root, "static", "search-index")
    
    print("=== Hugo Search Index Generator ===")
    print(f"Hugo root: {hugo_root}")
    print(f"Data source: {data_src_dir}")
    print(f"Output directory: {output_dir}")
    print()
    
    # Generate search indexes
    generate_search_indexes(data_src_dir, output_dir)
    
    print()
    print("=== Generation Complete ===")
    print("Remember to run 'hugo build' to include the search indexes in your site!")

if __name__ == "__main__":
    main()