#!/usr/bin/env python3
"""
Test script to verify column selection updates
"""

import sys
import os
sys.path.append('src')

from excel_filter.gui_new import ExcelFilterGUI
import tkinter as tk

def test_column_update():
    """Test that column selection updates are properly reflected in command display"""
    
    # Create main window
    root = tk.Tk()
    root.withdraw()  # Hide the main window for testing
    
    try:
        # Create the GUI
        app = ExcelFilterGUI(root)
        
        print("Testing column selection updates...")
        
        # Set up test data
        app.config_tab.input_file_var.set("test_input.xlsx")
        app.config_tab.output_file_var.set("test_output.xlsx")
        app.config_tab.pattern_var.set("test_pattern")
        app.config_tab.sheet_var.set("Sheet1")
        
        # Mock some column variables
        app.config_tab.columns_vars = {
            'Column1': tk.IntVar(value=1),
            'Column2': tk.IntVar(value=0),
            'Column3': tk.IntVar(value=1)
        }
        
        # Set up the connections (this should have been done in create_tabs)
        app.config_tab.set_execution_tab(app.execution_tab)
        app.config_tab.set_on_columns_changed(lambda: app.execution_tab.update_command_display())
        
        # Add tracing for individual column variables
        for var in app.config_tab.columns_vars.values():
            var.trace_add("write", lambda *args: app.execution_tab.update_command_display())
        
        print("Initial command display:")
        initial_command = app.execution_tab.command_var.get()
        print(f"Command: {initial_command}")
        
        # Change a column selection
        print("\nChanging Column2 selection...")
        app.config_tab.columns_vars['Column2'].set(1)
        root.update()  # Process the trace updates
        
        updated_command = app.execution_tab.command_var.get()
        print(f"Updated command: {updated_command}")
        
        # Check if columns appear in command
        columns_in_command = "Spalten:" in updated_command
        print(f"Columns appear in command: {columns_in_command}")
        
        if columns_in_command:
            print("[SUCCESS] Column selection update works!")
            return True
        else:
            print("[ERROR] Column selection update failed!")
            return False
        
    except Exception as e:
        print(f"Test failed with error: {e}")
        import traceback
        traceback.print_exc()
        return False
        
    finally:
        root.destroy()

if __name__ == "__main__":
    success = test_column_update()
    sys.exit(0 if success else 1)
