import os
import pytest
from unittest.mock import patch
import sys

# Set up path and environment before imports
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
os.environ['DB_ENV'] = 'test'

# Now import the module to test
from config import deletion_config


class TestDeletionConfig:
    """Tests for deletion configuration module."""
    
    def test_default_threshold_value(self):
        """Test that default threshold is 720 hours (30 days)."""
        # Reset to default by reloading module
        import importlib
        with patch.dict(os.environ, {}, clear=True):
            os.environ['DB_ENV'] = 'test'
            importlib.reload(deletion_config)
            assert deletion_config.ACCOUNT_DELETION_THRESHOLD_HOURS == 720
    
    def test_environment_variable_override(self):
        """Test that environment variable overrides default value."""
        import importlib
        with patch.dict(os.environ, {'ACCOUNT_DELETION_THRESHOLD_HOURS': '168', 'DB_ENV': 'test'}):
            importlib.reload(deletion_config)
            assert deletion_config.ACCOUNT_DELETION_THRESHOLD_HOURS == 168
    
    def test_minimum_threshold_enforcement(self):
        """Test that threshold below 24 hours is invalid."""
        with pytest.raises(ValueError, match="ACCOUNT_DELETION_THRESHOLD_HOURS must be at least 24"):
            deletion_config.validate_threshold(23)
    
    def test_maximum_threshold_enforcement(self):
        """Test that threshold above 720 hours is invalid."""
        with pytest.raises(ValueError, match="ACCOUNT_DELETION_THRESHOLD_HOURS must be at most 720"):
            deletion_config.validate_threshold(721)
    
    def test_invalid_threshold_negative(self):
        """Test that negative threshold values are invalid."""
        with pytest.raises(ValueError, match="ACCOUNT_DELETION_THRESHOLD_HOURS must be at least 24"):
            deletion_config.validate_threshold(-1)
    
    def test_invalid_threshold_zero(self):
        """Test that zero threshold is invalid."""
        with pytest.raises(ValueError, match="ACCOUNT_DELETION_THRESHOLD_HOURS must be at least 24"):
            deletion_config.validate_threshold(0)
    
    def test_valid_threshold_24_hours(self):
        """Test that 24 hours (minimum) is valid."""
        # Should not raise
        deletion_config.validate_threshold(24)
    
    def test_valid_threshold_720_hours(self):
        """Test that 720 hours (maximum) is valid."""
        # Should not raise
        deletion_config.validate_threshold(720)
    
    def test_valid_threshold_168_hours(self):
        """Test that 168 hours (7 days) is valid."""
        # Should not raise
        deletion_config.validate_threshold(168)
    
    def test_warning_for_threshold_below_168_hours(self, caplog):
        """Test that setting threshold below 168 hours logs a warning."""
        import logging
        caplog.set_level(logging.WARNING)
        deletion_config.validate_threshold(100)
        assert any("below the recommended minimum" in record.message for record in caplog.records)
    
    def test_no_warning_for_threshold_above_168_hours(self, caplog):
        """Test that threshold above 168 hours doesn't log warning."""
        import logging
        caplog.set_level(logging.WARNING)
        deletion_config.validate_threshold(200)
        # Should not have the specific warning
        assert not any("below the recommended minimum" in record.message for record in caplog.records)
    
    def test_threshold_constants_defined(self):
        """Test that MIN and MAX threshold constants are defined."""
        assert deletion_config.MIN_THRESHOLD_HOURS == 24
        assert deletion_config.MAX_THRESHOLD_HOURS == 720
    
    def test_invalid_environment_variable_non_numeric(self):
        """Test that non-numeric environment variable raises error."""
        import importlib
        with patch.dict(os.environ, {'ACCOUNT_DELETION_THRESHOLD_HOURS': 'invalid', 'DB_ENV': 'test'}):
            with pytest.raises(ValueError, match="ACCOUNT_DELETION_THRESHOLD_HOURS must be a valid integer"):
                importlib.reload(deletion_config)
    
    def test_environment_variable_with_decimal(self):
        """Test that decimal environment variable raises error."""
        import importlib
        with patch.dict(os.environ, {'ACCOUNT_DELETION_THRESHOLD_HOURS': '24.5', 'DB_ENV': 'test'}):
            with pytest.raises(ValueError, match="ACCOUNT_DELETION_THRESHOLD_HOURS must be a valid integer"):
                importlib.reload(deletion_config)
