# processors/process_move.py
# -*- coding: utf-8 -*-
import sympy
import traceback
import re # Importar re
from .processor_utils import get_sympy_representation, sympy_expr_to_scl, get_target_scl_name, format_variable_name
from .symbol_manager import SymbolManager

SCL_SUFFIX = "_sympy_processed"

def process_move(instruction, network_id, sympy_map, symbol_manager: SymbolManager, data):
    """Genera SCL para Move, simplificando la condición EN."""
    instr_uid = instruction["instruction_uid"]
    instr_type_original = instruction.get("type", "Move")
    if instr_type_original.endswith(SCL_SUFFIX) or "_error" in instr_type_original:
        return False

    # Obtener EN (SymPy) e IN (SymPy o Constante/String)
    en_input = instruction["inputs"].get("en")
    in_info = instruction["inputs"].get("in")
    sympy_en_expr = get_sympy_representation(en_input, network_id, sympy_map, symbol_manager) if en_input else sympy.true
    input_sympy_or_const = get_sympy_representation(in_info, network_id, sympy_map, symbol_manager)

    # Obtener destino SCL (requiere destino explícito para MOVE)
    target_scl_name = get_target_scl_name(instruction, "out1", network_id, default_to_temp=False)
    if target_scl_name is None:
        target_scl_name = get_target_scl_name(instruction, "out", network_id, default_to_temp=False)

    # Verificar dependencias
    if sympy_en_expr is None or input_sympy_or_const is None:
        return False
    if target_scl_name is None:
        print(f"Error: MOVE {instr_uid} sin destino claro en 'out' o 'out1'.")
        instruction["scl"] = f"// ERROR: MOVE {instr_uid} sin destino claro."
        instruction["type"] = instr_type_original + "_error"
        return True # Procesado con error

    # Convertir la entrada (SymPy o Constante) a SCL string
    input_scl = sympy_expr_to_scl(input_sympy_or_const, symbol_manager)

    # Generar SCL Core
    scl_core = f"{target_scl_name} := {input_scl};"

    # Aplicar Condición EN (Simplificando EN)
    scl_final = ""
    if sympy_en_expr != sympy.true:
        try:
            #simplified_en_expr = sympy.simplify_logic(sympy_en_expr, force=True)
            simplified_en_expr = sympy.logic.boolalg.to_dnf(sympy_en_expr, simplify=True)
        except Exception as e:
            print(f"Error simplifying EN for {instr_type_original} {instr_uid}: {e}")
            simplified_en_expr = sympy_en_expr # Fallback
        en_condition_scl = sympy_expr_to_scl(simplified_en_expr, symbol_manager)

        indented_core = "\n".join([f"  {line}" for line in scl_core.splitlines()])
        scl_final = f"IF {en_condition_scl} THEN\n{indented_core}\nEND_IF;"
    else:
        scl_final = scl_core

    # Actualizar instrucción y mapa
    instruction["scl"] = scl_final # SCL final generado
    instruction["type"] = instr_type_original + SCL_SUFFIX

    # Propagar valor de salida (nombre SCL del destino) y ENO (expresión SymPy)
    # Asumiendo que out y out1 deben propagar el mismo valor
    sympy_map[(network_id, instr_uid, "out")] = target_scl_name
    sympy_map[(network_id, instr_uid, "out1")] = target_scl_name
    sympy_map[(network_id, instr_uid, "eno")] = sympy_en_expr

    return True

# --- Processor Information Function ---
def get_processor_info():
    """Devuelve la información para la operación Move."""
    return {'type_name': 'move', 'processor_func': process_move, 'priority': 3}