#!/usr/bin/env python3
"""Fix the enrichment SQL to remove duplicate column assignments."""
import re
import sys

SQL_PATH = "/tmp/011-flexoptix-enrichment.sql"

with open(SQL_PATH, 'r') as f:
    content = f.read()

# Fix each UPDATE statement
def fix_update(match):
    full = match.group(0)
    # Extract SET clause
    set_match = re.search(r'SET\s+(.*?)\s+WHERE', full, re.S)
    if not set_match:
        return full
    set_clause = set_match.group(1)

    # Parse individual assignments
    assignments = re.split(r',\s*(?=[a-z_]+ =)', set_clause)
    seen = {}
    for a in assignments:
        a = a.strip()
        if not a:
            continue
        col_match = re.match(r'([a-z_]+)\s*=', a)
        if col_match:
            col = col_match.group(1)
            seen[col] = a  # last one wins

    if not seen:
        return full

    new_set = ', '.join(seen.values())
    result = re.sub(r'SET\s+.*?\s+WHERE', f'SET {new_set} WHERE', full, flags=re.S)
    return result

# Fix all UPDATE statements
fixed = re.sub(r'UPDATE transceivers SET .*? WHERE id = \'[^\']+\';', fix_update, content, flags=re.S)

# Also fix temp_range values
fixed = re.sub(r"temp_range = 'Commercial \(0°C to 70°C\)'", "temp_range = 'COM'", fixed)
fixed = re.sub(r"temp_range = 'Industrial \(-40°C to 85°C\)'", "temp_range = 'IND'", fixed)
fixed = re.sub(r"temp_range = 'Extended \(-5°C to 85°C\)'", "temp_range = 'EXT'", fixed)
fixed = re.sub(r"temp_range = 'Extended \(-40°C to 85°C\)'", "temp_range = 'IND'", fixed)

# Remove any temp_range that's not COM/IND/EXT
def fix_temp(m):
    val = m.group(1)
    if val in ('COM', 'IND', 'EXT'):
        return m.group(0)
    if 'ommercial' in val or '0°C to 70' in val:
        return "temp_range = 'COM'"
    if 'ndustrial' in val or '-40' in val:
        return "temp_range = 'IND'"
    if 'xtended' in val:
        return "temp_range = 'EXT'"
    return "temp_range = 'COM'"  # default

fixed = re.sub(r"temp_range = '([^']*)'", fix_temp, fixed)

with open(SQL_PATH, 'w') as f:
    f.write(fixed)

# Count updates
updates = len(re.findall(r'UPDATE transceivers', fixed))
print(f"Fixed {updates} UPDATE statements")
