"""
Test module for ExcelFilter
"""

import os
import sys
import pandas as pd

# Add src directory to path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))

from excel_filter.filter import ExcelFilter


class TestExcelFilter:
    """
    Test class for ExcelFilter
    """

    def setup_method(self):
        """
        Setup for the tests
        """
        import tempfile
        import os

        # Create temporary files
        self.temp_dir = tempfile.mkdtemp()
        self.test_input = os.path.join(self.temp_dir, "test_data.xlsx")
        self.test_output = os.path.join(self.temp_dir, "test_output.xlsx")

        # Create test data
        test_data = {
            'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
            'Status': ['Active', 'Inactive', 'Active', 'Inactive', 'Active'],
            'Message': ['Hello World', 'Error occurred', 'Warning: Low disk space', 'Critical failure', 'System running normally'],
            'Value': [100, 200, 150, 50, 300]
        }

        df = pd.DataFrame(test_data)
        df.to_excel(self.test_input, index=False)

    def teardown_method(self):
        """
        Cleanup after the tests
        """
        import shutil
        if os.path.exists(self.test_output):
            os.remove(self.test_output)
        if os.path.exists(self.test_input):
            os.remove(self.test_input)
        if hasattr(self, 'temp_dir') and os.path.exists(self.temp_dir):
            shutil.rmtree(self.temp_dir)

    def test_filter_error_pattern(self):
        """
        Test filtering with an error pattern
        """
        pattern = "error|warning|critical"
        excel_filter = ExcelFilter(
            input_file=self.test_input,
            output_file=self.test_output,
            pattern=pattern
        )

        success = excel_filter.process()
        assert success

        # Verify output file
        result_df = pd.read_excel(self.test_output)
        assert len(result_df) == 3  # Bob, Charlie, David
        
    def test_filter_specific_column(self):
        """
        Test filtering in a specific column
        """
        pattern = "active"
        excel_filter = ExcelFilter(
            input_file=self.test_input,
            output_file=self.test_output,
            pattern=pattern,
            columns=['Status']
        )

        success = excel_filter.process()
        assert success

        # Verify output file
        result_df = pd.read_excel(self.test_output)
        assert len(result_df) == 3  # Alice, Charlie, Eve
        
    def test_filter_value_pattern(self):
        """
        Test filtering with a numeric pattern
        """
        pattern = "1\d{2}"  # Numbers between 100-199
        excel_filter = ExcelFilter(
            input_file=self.test_input,
            output_file=self.test_output,
            pattern=pattern,
            columns=['Value']
        )

        success = excel_filter.process()
        assert success

        # Verify output file
        result_df = pd.read_excel(self.test_output)
        assert len(result_df) == 2  # Alice (100), Charlie (150)

    def test_filter_no_matches(self):
        """
        Test filtering with a pattern that finds no matches
        """
        pattern = "nonexistent"
        excel_filter = ExcelFilter(
            input_file=self.test_input,
            output_file=self.test_output,
            pattern=pattern
        )

        success = excel_filter.process()
        assert success

        # Verify output file
        result_df = pd.read_excel(self.test_output)
        assert len(result_df) == 0  # No matches

    def test_filter_invalid_file(self):
        """
        Test filtering with an invalid file
        """
        pattern = "error"
        excel_filter = ExcelFilter(
            input_file="nonexistent.xlsx",
            output_file=self.test_output,
            pattern=pattern
        )

        success = excel_filter.process()
        assert not success

    def test_filter_empty_pattern(self):
        """
        Test filtering with an empty pattern
        """
        pattern = ""
        excel_filter = ExcelFilter(
            input_file=self.test_input,
            output_file=self.test_output,
            pattern=pattern
        )

        success = excel_filter.process()
        assert success

        # Verify output file
        result_df = pd.read_excel(self.test_output)
        assert len(result_df) == len(pd.read_excel(self.test_input))  # All rows
