"""
Unit tests for main.py
"""

import unittest
import json
import tempfile
import os
from unittest.mock import patch
from excel_filter.main import load_config, main


class TestMain(unittest.TestCase):
    """
    Test class for main module functions
    """

    def test_load_config_success(self):
        """
        Test successful config loading
        """
        config_data = {
            "pattern": "test.*",
            "sheet_name": "Sheet1",
            "columns": ["A", "B"]
        }

        with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
            json.dump(config_data, f)
            config_file = f.name

        try:
            result = load_config(config_file)
            self.assertEqual(result, config_data)
        finally:
            os.unlink(config_file)

    def test_load_config_file_not_found(self):
        """
        Test config loading with non-existent file
        """
        with self.assertRaises(Exception):
            load_config("nonexistent.json")

    @patch('sys.argv', ['main.py', '--input', 'input.xlsx', '--output', 'output.xlsx', '--pattern', 'test'])
    @patch('excel_filter.main.ExcelFilter')
    def test_main_with_args(self, mock_filter_class):
        """
        Test main function with command line arguments
        """
        mock_filter = mock_filter_class.return_value
        mock_filter.process.return_value = True

        main()

        mock_filter_class.assert_called_once_with(
            input_file='input.xlsx',
            output_file='output.xlsx',
            pattern='test',
            sheet_name=None,
            columns=None
        )
        mock_filter.process.assert_called_once()

    @patch('sys.argv', ['main.py', '--input', 'input.xlsx', '--output', 'output.xlsx', '--config', 'config.json'])
    @patch('excel_filter.main.load_config')
    @patch('excel_filter.main.ExcelFilter')
    def test_main_with_config(self, mock_filter_class, mock_load_config):
        """
        Test main function with config file
        """
        mock_load_config.return_value = {
            "pattern": "config_pattern",
            "sheet_name": "ConfigSheet",
            "columns": ["Col1", "Col2"]
        }
        mock_filter = mock_filter_class.return_value
        mock_filter.process.return_value = True

        main()

        mock_load_config.assert_called_once_with('config.json')
        mock_filter_class.assert_called_once_with(
            input_file='input.xlsx',
            output_file='output.xlsx',
            pattern='config_pattern',
            sheet_name='ConfigSheet',
            columns=['Col1', 'Col2']
        )
        mock_filter.process.assert_called_once()

    @patch('sys.argv', ['main.py', '--input', 'input.xlsx', '--output', 'output.xlsx'])
    @patch('excel_filter.main.logger')
    def test_main_no_pattern(self, mock_logger):
        """
        Test main function with no pattern specified
        """
        main()

        mock_logger.error.assert_called_once_with("Kein Regex-Muster angegeben")


if __name__ == '__main__':
    unittest.main()
