#!/usr/bin/env python3


import sys
import os
import tempfile
import json
import pytest
from unittest.mock import Mock, patch, MagicMock
from pathlib import Path

# Add the parent directory to the path for imports
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

import tkinter as tk
from gui_new import ExcelFilterGUI


@pytest.fixture
def mock_root():
    """Create a mock tkinter root window"""
    root = Mock(spec=tk.Tk)
    root.title = Mock()
    root.configure = Mock()
    root.iconbitmap = Mock()
    root.destroy = Mock()
    root.update = Mock()
    return root


@pytest.fixture
def temp_files():
    """Create temporary files for testing"""
    with tempfile.TemporaryDirectory() as temp_dir:
        # Create a temporary config file
        config_file = os.path.join(temp_dir, 'config.json')
        with open(config_file, 'w') as f:
            json.dump({
                'input_file': 'test_input.xlsx',
                'output_file': 'test_output.xlsx',
                'pattern': 'error|warning',
                'sheet_name': 'Sheet1',
                'columns': ['Column1', 'Column2']
            }, f)

        # Create a temporary presets file
        presets_file = os.path.join(temp_dir, 'presets.json')
        with open(presets_file, 'w') as f:
            json.dump({
                'presets': {'test_pattern': 'test.*'},
                'descriptions': {'test_pattern': 'Test pattern'}
            }, f)

        yield temp_dir, config_file, presets_file


class TestExcelFilterGUI:

    def test_update_columns_selection_signature(self):
        """Test that update_columns_selection method accepts selected_columns parameter"""
        # Test the method signature directly without full GUI initialization
        import inspect
        method = getattr(ExcelFilterGUI, 'update_columns_selection')
        sig = inspect.signature(method)

        # Check if 'selected_columns' parameter exists
        assert 'selected_columns' in sig.parameters
        param = sig.parameters['selected_columns']
        assert param.default is None

    def test_color_palette(self):
        """Test that Windows 11 color palette is properly defined"""
        # Test colors without full initialization
        expected_colors = ['primary', 'primary_dark', 'primary_light', 'background',
                          'surface', 'text', 'text_secondary', 'border', 'hover', 'pressed']

        # Check that all expected colors are defined
        for color in expected_colors:
            assert hasattr(ExcelFilterGUI, 'win11_colors') or color in ExcelFilterGUI().win11_colors

    def test_file_paths(self):
        """Test that file paths are correctly set"""
        gui = ExcelFilterGUI.__new__(ExcelFilterGUI)  # Create instance without __init__
        gui.__init__ = Mock()  # Mock __init__ to avoid full initialization

        # Manually set attributes for testing
        gui.config_file = "config.json"
        gui.presets_file = "presets.json"

        assert gui.config_file == "config.json"
        assert gui.presets_file == "presets.json"

    @patch('gui_new.MainWindow')
    @patch('gui_new.ConfigTab')
    @patch('gui_new.ExecutionTab')
    @patch('gui_new.RegexBuilderTab')
    @patch('gui_new.HelpTab')
    def test_create_tabs(self, mock_help_tab, mock_regex_tab, mock_execution_tab,
                        mock_config_tab, mock_main_window, mock_root):
        """Test tab creation"""
        # Setup mocks similar to initialization test
        mock_main_window_instance = Mock()
        mock_main_window_instance.notebook = Mock()
        mock_main_window_instance.scale_factor = 1.0
        mock_main_window_instance.language_frame = Mock()
        mock_main_window_instance.enhance_tab_appearance = Mock()
        mock_main_window.return_value = mock_main_window_instance

        mock_config_tab_instance = Mock()
        mock_config_tab_instance.get_frame.return_value = Mock()
        mock_config_tab_instance.set_execution_tab = Mock()
        mock_config_tab_instance.set_main_gui = Mock()
        mock_config_tab_instance.set_on_columns_changed = Mock()
        mock_config_tab.return_value = mock_config_tab_instance

        mock_execution_tab_instance = Mock()
        mock_execution_tab_instance.get_frame.return_value = Mock()
        mock_execution_tab_instance.set_config_tab = Mock()
        mock_execution_tab_instance.set_main_gui = Mock()
        mock_execution_tab_instance.update_command_display = Mock()
        mock_execution_tab.return_value = mock_execution_tab_instance

        mock_regex_tab_instance = Mock()
        mock_regex_tab_instance.get_frame.return_value = Mock()
        mock_regex_tab.return_value = mock_regex_tab_instance

        mock_help_tab_instance = Mock()
        mock_help_tab_instance.get_frame.return_value = Mock()
        mock_help_tab.return_value = mock_help_tab_instance

        with patch('gui_new.Translations') as mock_translations:
            mock_translations_instance = Mock()
            mock_translations_instance.__getitem__ = Mock(return_value='Test')
            mock_translations_instance.app_title = 'Test App'
            mock_translations.return_value = mock_translations_instance

            with patch('builtins.open', Mock()):
                with patch('json.load', return_value={'presets': {}, 'descriptions': {}}):
                    gui = ExcelFilterGUI(mock_root)

                    # Verify tabs were added to notebook
                    assert mock_main_window_instance.notebook.add.call_count == 4

                    # Verify config tab connections
                    assert mock_config_tab_instance.browse_input_file is not None
                    assert mock_config_tab_instance.browse_output_file is not None
                    assert mock_config_tab_instance.update_columns_selection is not None
                    assert mock_config_tab_instance.select_all_columns is not None
                    assert mock_config_tab_instance.deselect_all_columns is not None

    @patch('gui_new.MainWindow')
    @patch('gui_new.ConfigTab')
    @patch('gui_new.ExecutionTab')
    @patch('gui_new.RegexBuilderTab')
    @patch('gui_new.HelpTab')
    def test_update_columns_selection_no_file(self, mock_help_tab, mock_regex_tab,
                                            mock_execution_tab, mock_config_tab,
                                            mock_main_window, mock_root):
        """Test update_columns_selection with no input file"""
        # Setup basic mocks
        self._setup_basic_mocks(mock_help_tab, mock_regex_tab, mock_execution_tab,
                               mock_config_tab, mock_main_window, mock_root)

        with patch('gui_new.Translations') as mock_translations:
            mock_translations_instance = Mock()
            mock_translations_instance.__getitem__ = Mock(return_value='Test')
            mock_translations_instance.app_title = 'Test App'
            mock_translations.return_value = mock_translations_instance

            with patch('builtins.open', Mock()):
                with patch('json.load', return_value={'presets': {}, 'descriptions': {}}):
                    with patch('tkinter.messagebox.showerror') as mock_error:
                        gui = ExcelFilterGUI(mock_root)

                        # Set empty input file
                        gui.config_tab.input_file_var.get.return_value = ""

                        # Call update_columns_selection
                        gui.update_columns_selection()

                        # Verify error message was shown
                        mock_error.assert_called_once()
                        args = mock_error.call_args[0]
                        assert "Bitte geben Sie eine Eingabedatei an" in args[1]

    @patch('gui_new.MainWindow')
    @patch('gui_new.ConfigTab')
    @patch('gui_new.ExecutionTab')
    @patch('gui_new.RegexBuilderTab')
    @patch('gui_new.HelpTab')
    @patch('pandas.read_excel')
    def test_update_columns_selection_with_file(self, mock_read_excel, mock_help_tab,
                                              mock_regex_tab, mock_execution_tab,
                                              mock_config_tab, mock_main_window, mock_root):
        """Test update_columns_selection with valid input file"""
        # Setup basic mocks
        self._setup_basic_mocks(mock_help_tab, mock_regex_tab, mock_execution_tab,
                               mock_config_tab, mock_main_window, mock_root)

        # Mock pandas DataFrame
        mock_df = Mock()
        mock_df.columns.tolist.return_value = ['Column1', 'Column2', 'Column3']
        mock_read_excel.return_value = mock_df

        with patch('gui_new.Translations') as mock_translations:
            mock_translations_instance = Mock()
            mock_translations_instance.__getitem__ = Mock(return_value='Test')
            mock_translations_instance.app_title = 'Test App'
            mock_translations.return_value = mock_translations_instance

            with patch('builtins.open', Mock()):
                with patch('json.load', return_value={'presets': {}, 'descriptions': {}}):
                    with patch('tkinter.ttk.Checkbutton') as mock_checkbutton:
                        gui = ExcelFilterGUI(mock_root)

                        # Set valid input file
                        gui.config_tab.input_file_var.get.return_value = "test.xlsx"
                        gui.config_tab.sheet_var.get.return_value = "Sheet1"

                        # Mock container operations
                        mock_container = Mock()
                        gui.config_tab.columns_container = mock_container
                        mock_container.winfo_children.return_value = []
                        mock_container.grid = Mock()
                        mock_container.update_idletasks = Mock()

                        # Call update_columns_selection
                        gui.update_columns_selection()

                        # Verify pandas was called
                        mock_read_excel.assert_called_once_with("test.xlsx", sheet_name="Sheet1")

                        # Verify checkboxes were created (3 columns)
                        assert mock_checkbutton.call_count == 3

                        # Verify regex column combobox was updated
                        gui.config_tab.regex_column_combobox.__setitem__.assert_called_with(
                            'values', ['Alle Spalten', 'Column1', 'Column2', 'Column3']
                        )

    @patch('gui_new.MainWindow')
    @patch('gui_new.ConfigTab')
    @patch('gui_new.ExecutionTab')
    @patch('gui_new.RegexBuilderTab')
    @patch('gui_new.HelpTab')
    def test_update_columns_selection_with_selected_columns(self, mock_help_tab, mock_regex_tab,
                                                          mock_execution_tab, mock_config_tab,
                                                          mock_main_window, mock_root):
        """Test update_columns_selection with pre-selected columns"""
        # Setup basic mocks
        self._setup_basic_mocks(mock_help_tab, mock_regex_tab, mock_execution_tab,
                               mock_config_tab, mock_main_window, mock_root)

        with patch('pandas.read_excel') as mock_read_excel:
            mock_df = Mock()
            mock_df.columns.tolist.return_value = ['Column1', 'Column2', 'Column3']
            mock_read_excel.return_value = mock_df

            with patch('gui_new.Translations') as mock_translations:
                mock_translations_instance = Mock()
                mock_translations_instance.__getitem__ = Mock(return_value='Test')
                mock_translations_instance.app_title = 'Test App'
                mock_translations.return_value = mock_translations_instance

                with patch('builtins.open', Mock()):
                    with patch('json.load', return_value={'presets': {}, 'descriptions': {}}):
                        with patch('tkinter.ttk.Checkbutton') as mock_checkbutton:
                            gui = ExcelFilterGUI(mock_root)

                            # Set valid input file
                            gui.config_tab.input_file_var.get.return_value = "test.xlsx"
                            gui.config_tab.sheet_var.get.return_value = "Sheet1"

                            # Mock container
                            mock_container = Mock()
                            gui.config_tab.columns_container = mock_container
                            mock_container.winfo_children.return_value = []
                            mock_container.grid = Mock()
                            mock_container.update_idletasks = Mock()

                            # Create mock variables for columns
                            mock_vars = {}
                            for i, col in enumerate(['Column1', 'Column2', 'Column3']):
                                mock_var = Mock()
                                mock_vars[col] = mock_var
                                gui.config_tab.columns_vars = mock_vars

                            # Call with selected columns
                            selected_columns = ['Column1', 'Column3']
                            gui.update_columns_selection(selected_columns=selected_columns)

                            # Verify that selected columns were set
                            mock_vars['Column1'].set.assert_called_with(1)
                            mock_vars['Column3'].set.assert_called_with(1)
                            # Column2 should not be set (not in selected_columns)
                            mock_vars['Column2'].set.assert_not_called()

    @patch('gui_new.MainWindow')
    @patch('gui_new.ConfigTab')
    @patch('gui_new.ExecutionTab')
    @patch('gui_new.RegexBuilderTab')
    @patch('gui_new.HelpTab')
    def test_select_all_columns(self, mock_help_tab, mock_regex_tab, mock_execution_tab,
                               mock_config_tab, mock_main_window, mock_root):
        """Test select_all_columns method"""
        # Setup basic mocks
        self._setup_basic_mocks(mock_help_tab, mock_regex_tab, mock_execution_tab,
                               mock_config_tab, mock_main_window, mock_root)

        with patch('gui_new.Translations') as mock_translations:
            mock_translations_instance = Mock()
            mock_translations_instance.__getitem__ = Mock(return_value='Test')
            mock_translations_instance.app_title = 'Test App'
            mock_translations.return_value = mock_translations_instance

            with patch('builtins.open', Mock()):
                with patch('json.load', return_value={'presets': {}, 'descriptions': {}}):
                    gui = ExcelFilterGUI(mock_root)

                    # Setup mock column variables
                    mock_var1 = Mock()
                    mock_var2 = Mock()
                    gui.config_tab.columns_vars = {
                        'Column1': mock_var1,
                        'Column2': mock_var2
                    }

                    # Call select_all_columns
                    gui.select_all_columns()

                    # Verify all variables were set to 1
                    mock_var1.set.assert_called_with(1)
                    mock_var2.set.assert_called_with(1)

    @patch('gui_new.MainWindow')
    @patch('gui_new.ConfigTab')
    @patch('gui_new.ExecutionTab')
    @patch('gui_new.RegexBuilderTab')
    @patch('gui_new.HelpTab')
    def test_deselect_all_columns(self, mock_help_tab, mock_regex_tab, mock_execution_tab,
                                 mock_config_tab, mock_main_window, mock_root):
        """Test deselect_all_columns method"""
        # Setup basic mocks
        self._setup_basic_mocks(mock_help_tab, mock_regex_tab, mock_execution_tab,
                               mock_config_tab, mock_main_window, mock_root)

        with patch('gui_new.Translations') as mock_translations:
            mock_translations_instance = Mock()
            mock_translations_instance.__getitem__ = Mock(return_value='Test')
            mock_translations_instance.app_title = 'Test App'
            mock_translations.return_value = mock_translations_instance

            with patch('builtins.open', Mock()):
                with patch('json.load', return_value={'presets': {}, 'descriptions': {}}):
                    gui = ExcelFilterGUI(mock_root)

                    # Setup mock column variables
                    mock_var1 = Mock()
                    mock_var2 = Mock()
                    gui.config_tab.columns_vars = {
                        'Column1': mock_var1,
                        'Column2': mock_var2
                    }

                    # Call deselect_all_columns
                    gui.deselect_all_columns()

                    # Verify all variables were set to 0
                    mock_var1.set.assert_called_with(0)
                    mock_var2.set.assert_called_with(0)

    @patch('gui_new.MainWindow')
    @patch('gui_new.ConfigTab')
    @patch('gui_new.ExecutionTab')
    @patch('gui_new.RegexBuilderTab')
    @patch('gui_new.HelpTab')
    @patch('gui_new.browse_file')
    def test_browse_input_file(self, mock_browse_file, mock_help_tab, mock_regex_tab,
                              mock_execution_tab, mock_config_tab, mock_main_window, mock_root):
        """Test browse_input_file method"""
        # Setup basic mocks
        self._setup_basic_mocks(mock_help_tab, mock_regex_tab, mock_execution_tab,
                               mock_config_tab, mock_main_window, mock_root)

        mock_browse_file.return_value = "/path/to/test.xlsx"

        with patch('gui_new.Translations') as mock_translations:
            mock_translations_instance = Mock()
            mock_translations_instance.__getitem__ = Mock(return_value='Test')
            mock_translations_instance.app_title = 'Test App'
            mock_translations.return_value = mock_translations_instance

            with patch('builtins.open', Mock()):
                with patch('json.load', return_value={'presets': {}, 'descriptions': {}}):
                    gui = ExcelFilterGUI(mock_root)

                    # Mock the methods that will be called
                    gui.update_sheet_selection = Mock()
                    gui.update_columns_selection = Mock()

                    # Call browse_input_file
                    gui.browse_input_file()

                    # Verify file was set
                    gui.config_tab.input_file_var.set.assert_called_with("/path/to/test.xlsx")

                    # Verify update methods were called
                    gui.update_sheet_selection.assert_called_once()
                    gui.update_columns_selection.assert_called_once()

                    # Verify output file was auto-filled
                    expected_output = "/path/to/test_filtered.xlsx"
                    gui.config_tab.output_file_var.set.assert_called_with(expected_output)

    @patch('gui_new.MainWindow')
    @patch('gui_new.ConfigTab')
    @patch('gui_new.ExecutionTab')
    @patch('gui_new.RegexBuilderTab')
    @patch('gui_new.HelpTab')
    @patch('gui_new.browse_save_file')
    def test_browse_output_file(self, mock_browse_save_file, mock_help_tab, mock_regex_tab,
                               mock_execution_tab, mock_config_tab, mock_main_window, mock_root):
        """Test browse_output_file method"""
        # Setup basic mocks
        self._setup_basic_mocks(mock_help_tab, mock_regex_tab, mock_execution_tab,
                               mock_config_tab, mock_main_window, mock_root)

        mock_browse_save_file.return_value = "/path/to/output.xlsx"

        with patch('gui_new.Translations') as mock_translations:
            mock_translations_instance = Mock()
            mock_translations_instance.__getitem__ = Mock(return_value='Test')
            mock_translations_instance.app_title = 'Test App'
            mock_translations.return_value = mock_translations_instance

            with patch('builtins.open', Mock()):
                with patch('json.load', return_value={'presets': {}, 'descriptions': {}}):
                    gui = ExcelFilterGUI(mock_root)

                    # Call browse_output_file
                    gui.browse_output_file()

                    # Verify file was set
                    gui.config_tab.output_file_var.set.assert_called_with("/path/to/output.xlsx")

    @patch('gui_new.MainWindow')
    @patch('gui_new.ConfigTab')
    @patch('gui_new.ExecutionTab')
    @patch('gui_new.RegexBuilderTab')
    @patch('gui_new.HelpTab')
    @patch('gui_new.load_config')
    def test_load_config(self, mock_load_config, mock_help_tab, mock_regex_tab,
                        mock_execution_tab, mock_config_tab, mock_main_window, mock_root):
        """Test load_config method"""
        # Setup basic mocks
        self._setup_basic_mocks(mock_help_tab, mock_regex_tab, mock_execution_tab,
                               mock_config_tab, mock_main_window, mock_root)

        mock_load_config.return_value = {
            'input_file': 'test.xlsx',
            'output_file': 'output.xlsx',
            'pattern': 'error.*',
            'sheet_name': 'Sheet1',
            'columns': ['Col1', 'Col2']
        }

        with patch('gui_new.Translations') as mock_translations:
            mock_translations_instance = Mock()
            mock_translations_instance.__getitem__ = Mock(return_value='Test')
            mock_translations_instance.app_title = 'Test App'
            mock_translations.return_value = mock_translations_instance

            with patch('builtins.open', Mock()):
                with patch('json.load', return_value={'presets': {}, 'descriptions': {}}):
                    with patch('tkinter.messagebox.showinfo') as mock_info:
                        with patch('os.path.exists', return_value=True):
                            gui = ExcelFilterGUI(mock_root)

                            # Mock update methods
                            gui.update_sheet_selection = Mock()
                            gui.update_columns_selection = Mock()

                            # Call load_config
                            gui.load_config()

                            # Verify config values were set
                            gui.config_tab.input_file_var.set.assert_called_with('test.xlsx')
                            gui.config_tab.output_file_var.set.assert_called_with('output.xlsx')
                            gui.config_tab.pattern_var.set.assert_called_with('error.*')
                            gui.config_tab.sheet_var.set.assert_called_with('Sheet1')

                            # Verify update methods were called
                            gui.update_sheet_selection.assert_called_once()
                            gui.update_columns_selection.assert_called_once_with(selected_columns=['Col1', 'Col2'])

                            # Verify success message
                            mock_info.assert_called_once()

    @patch('gui_new.MainWindow')
    @patch('gui_new.ConfigTab')
    @patch('gui_new.ExecutionTab')
    @patch('gui_new.RegexBuilderTab')
    @patch('gui_new.HelpTab')
    @patch('gui_new.save_config')
    def test_save_config(self, mock_save_config, mock_help_tab, mock_regex_tab,
                        mock_execution_tab, mock_config_tab, mock_main_window, mock_root):
        """Test save_config method"""
        # Setup basic mocks
        self._setup_basic_mocks(mock_help_tab, mock_regex_tab, mock_execution_tab,
                               mock_config_tab, mock_main_window, mock_root)

        with patch('gui_new.Translations') as mock_translations:
            mock_translations_instance = Mock()
            mock_translations_instance.__getitem__ = Mock(return_value='Test')
            mock_translations_instance.app_title = 'Test App'
            mock_translations.return_value = mock_translations_instance

            with patch('builtins.open', Mock()):
                with patch('json.load', return_value={'presets': {}, 'descriptions': {}}):
                    with patch('tkinter.messagebox.showinfo') as mock_info:
                        gui = ExcelFilterGUI(mock_root)

                        # Setup mock return values
                        gui.config_tab.input_file_var.get.return_value = 'input.xlsx'
                        gui.config_tab.output_file_var.get.return_value = 'output.xlsx'
                        gui.config_tab.pattern_var.get.return_value = 'pattern.*'
                        gui.config_tab.sheet_var.get.return_value = 'Sheet1'
                        gui.config_tab.columns_var.get.return_value = 'columns'

                        # Call save_config
                        gui.save_config()

                        # Verify save_config was called with correct data
                        expected_config = {
                            'input_file': 'input.xlsx',
                            'output_file': 'output.xlsx',
                            'pattern': 'pattern.*',
                            'sheet_name': 'Sheet1',
                            'columns': 'columns'
                        }
                        mock_save_config.assert_called_once_with("config.json", expected_config)

                        # Verify success message
                        mock_info.assert_called_once()

    @patch('gui_new.MainWindow')
    @patch('gui_new.ConfigTab')
    @patch('gui_new.ExecutionTab')
    @patch('gui_new.RegexBuilderTab')
    @patch('gui_new.HelpTab')
    def test_load_presets(self, mock_help_tab, mock_regex_tab, mock_execution_tab,
                         mock_config_tab, mock_main_window, mock_root):
        """Test load_presets method"""
        # Setup basic mocks
        self._setup_basic_mocks(mock_help_tab, mock_regex_tab, mock_execution_tab,
                               mock_config_tab, mock_main_window, mock_root)

        with patch('gui_new.Translations') as mock_translations:
            mock_translations_instance = Mock()
            mock_translations_instance.__getitem__ = Mock(return_value='Default Pattern')
            mock_translations_instance.app_title = 'Test App'
            mock_translations.return_value = mock_translations_instance

            with patch('builtins.open', Mock()):
                with patch('json.load', return_value={'presets': {}, 'descriptions': {}}):
                    with patch('os.path.exists', return_value=True):
                        with patch('gui_new.save_presets') as mock_save_presets:
                            gui = ExcelFilterGUI(mock_root)

                            # Call load_presets
                            presets, descriptions = gui.load_presets()

                            # Verify presets were loaded
                            assert isinstance(presets, dict)
                            assert isinstance(descriptions, dict)

    @patch('gui_new.MainWindow')
    @patch('gui_new.ConfigTab')
    @patch('gui_new.ExecutionTab')
    @patch('gui_new.RegexBuilderTab')
    @patch('gui_new.HelpTab')
    def test_save_presets(self, mock_help_tab, mock_regex_tab, mock_execution_tab,
                         mock_config_tab, mock_main_window, mock_root):
        """Test save_presets method"""
        # Setup basic mocks
        self._setup_basic_mocks(mock_help_tab, mock_regex_tab, mock_execution_tab,
                               mock_config_tab, mock_main_window, mock_root)

        with patch('gui_new.Translations') as mock_translations:
            mock_translations_instance = Mock()
            mock_translations_instance.__getitem__ = Mock(return_value='Test')
            mock_translations_instance.app_title = 'Test App'
            mock_translations.return_value = mock_translations_instance

            with patch('builtins.open', Mock()):
                with patch('json.load', return_value={'presets': {}, 'descriptions': {}}):
                    with patch('gui_new.open', Mock()) as mock_open:
                        with patch('json.dump') as mock_dump:
                            gui = ExcelFilterGUI(mock_root)

                            # Set up test presets
                            gui.pattern_presets = {'test': 'pattern'}
                            gui.pattern_descriptions = {'test': 'description'}

                            # Call save_presets
                            gui.save_presets()

                            # Verify json.dump was called
                            mock_dump.assert_called_once()
                            args = mock_dump.call_args[0]
                            assert args[0]['presets'] == {'test': 'pattern'}
                            assert args[0]['descriptions'] == {'test': 'description'}

    @patch('gui_new.MainWindow')
    @patch('gui_new.ConfigTab')
    @patch('gui_new.ExecutionTab')
    @patch('gui_new.RegexBuilderTab')
    @patch('gui_new.HelpTab')
    def test_open_file_windows(self, mock_help_tab, mock_regex_tab, mock_execution_tab,
                              mock_config_tab, mock_main_window, mock_root):
        """Test open_file method on Windows"""
        # Setup basic mocks
        self._setup_basic_mocks(mock_help_tab, mock_regex_tab, mock_execution_tab,
                               mock_config_tab, mock_main_window, mock_root)

        with patch('gui_new.Translations') as mock_translations:
            mock_translations_instance = Mock()
            mock_translations_instance.__getitem__ = Mock(return_value='Test')
            mock_translations_instance.app_title = 'Test App'
            mock_translations.return_value = mock_translations_instance

            with patch('builtins.open', Mock()):
                with patch('json.load', return_value={'presets': {}, 'descriptions': {}}):
                    with patch('sys.platform', 'win32'):
                        with patch('os.startfile') as mock_startfile:
                            gui = ExcelFilterGUI(mock_root)

                            # Call open_file
                            gui.open_file('test.xlsx')

                            # Verify os.startfile was called
                            mock_startfile.assert_called_once_with('test.xlsx')

    @patch('gui_new.MainWindow')
    @patch('gui_new.ConfigTab')
    @patch('gui_new.ExecutionTab')
    @patch('gui_new.RegexBuilderTab')
    @patch('gui_new.HelpTab')
    def test_open_file_unix(self, mock_help_tab, mock_regex_tab, mock_execution_tab,
                           mock_config_tab, mock_main_window, mock_root):
        """Test open_file method on Unix-like systems"""
        # Setup basic mocks
        self._setup_basic_mocks(mock_help_tab, mock_regex_tab, mock_execution_tab,
                               mock_config_tab, mock_main_window, mock_root)

        with patch('gui_new.Translations') as mock_translations:
            mock_translations_instance = Mock()
            mock_translations_instance.__getitem__ = Mock(return_value='Test')
            mock_translations_instance.app_title = 'Test App'
            mock_translations.return_value = mock_translations_instance

            with patch('builtins.open', Mock()):
                with patch('json.load', return_value={'presets': {}, 'descriptions': {}}):
                    with patch('sys.platform', 'linux'):
                        with patch('subprocess.run') as mock_run:
                            gui = ExcelFilterGUI(mock_root)

                            # Call open_file
                            gui.open_file('test.xlsx')

                            # Verify subprocess.run was called with xdg-open
                            mock_run.assert_called_once_with(['xdg-open', 'test.xlsx'])

    def _setup_basic_mocks(self, mock_help_tab, mock_regex_tab, mock_execution_tab,
                          mock_config_tab, mock_main_window, mock_root):
        """Helper method to setup basic mocks for tests"""
        # Setup MainWindow mock
        mock_main_window_instance = Mock()
        mock_main_window_instance.notebook = Mock()
        mock_main_window_instance.scale_factor = 1.0
        mock_main_window_instance.language_frame = Mock()
        mock_main_window_instance.enhance_tab_appearance = Mock()
        mock_main_window.return_value = mock_main_window_instance

        # Setup ConfigTab mock
        mock_config_tab_instance = Mock()
        mock_config_frame = Mock()
        mock_config_frame.winfo_children.return_value = []  # Make it iterable
        mock_config_tab_instance.frame = mock_config_frame
        mock_config_tab_instance.get_frame.return_value = mock_config_frame
        mock_config_tab_instance.set_execution_tab = Mock()
        mock_config_tab_instance.set_main_gui = Mock()
        mock_config_tab_instance.set_on_columns_changed = Mock()
        mock_config_tab_instance.input_file_var = Mock()
        mock_config_tab_instance.output_file_var = Mock()
        mock_config_tab_instance.pattern_var = Mock()
        mock_config_tab_instance.sheet_var = Mock()
        mock_config_tab_instance.columns_var = Mock()
        mock_config_tab_instance.regex_column_combobox = Mock()
        mock_config_tab_instance.columns_container = Mock()
        mock_config_tab_instance.columns_vars = {}
        mock_config_tab_instance.log_message = Mock()
        mock_config_tab_instance.log_error = Mock()
        mock_config_tab_instance.status_var = Mock()
        mock_config_tab.return_value = mock_config_tab_instance

        # Setup ExecutionTab mock
        mock_execution_tab_instance = Mock()
        mock_execution_tab_instance.get_frame.return_value = Mock()
        mock_execution_tab_instance.set_config_tab = Mock()
        mock_execution_tab_instance.set_main_gui = Mock()
        mock_execution_tab_instance.update_command_display = Mock()
        mock_execution_tab.return_value = mock_execution_tab_instance

        # Setup RegexBuilderTab mock
        mock_regex_tab_instance = Mock()
        mock_regex_tab_instance.get_frame.return_value = Mock()
        mock_regex_tab.return_value = mock_regex_tab_instance

        # Setup HelpTab mock
        mock_help_tab_instance = Mock()
        mock_help_tab_instance.get_frame.return_value = Mock()
        mock_help_tab.return_value = mock_help_tab_instance


if __name__ == "__main__":
    pytest.main([__file__, "-v"])
