"""
Unit tests for filter service (without database).

These tests verify the filter service logic without requiring database setup.
"""

from django.test import TestCase, RequestFactory
from datetime import date
from reports.filter_service import ReportFilterService, FilterParams


class TestFilterParamsParsing(TestCase):
    """Test parsing of filter parameters from HTTP requests"""
    
    def setUp(self):
        self.factory = RequestFactory()
    
    def test_parse_date_range_from_get_request(self):
        """Test parsing date range from GET parameters"""
        request = self.factory.get('/', {
            'start_date': '2024-01-01',
            'end_date': '2024-12-31'
        })
        
        params = ReportFilterService.parse_filter_params(request)
        
        self.assertEqual(params.start_date, date(2024, 1, 1))
        self.assertEqual(params.end_date, date(2024, 12, 31))
    
    def test_parse_date_range_from_post_request(self):
        """Test parsing date range from POST parameters"""
        request = self.factory.post('/', {
            'start_date': '2024-06-01',
            'end_date': '2024-06-30'
        })
        
        params = ReportFilterService.parse_filter_params(request)
        
        self.assertEqual(params.start_date, date(2024, 6, 1))
        self.assertEqual(params.end_date, date(2024, 6, 30))
    
    def test_parse_invalid_date_format(self):
        """Test handling of invalid date format"""
        request = self.factory.get('/', {
            'start_date': 'invalid-date',
            'end_date': '2024-12-31'
        })
        
        params = ReportFilterService.parse_filter_params(request)
        
        self.assertIsNone(params.start_date)
        self.assertEqual(params.end_date, date(2024, 12, 31))
    
    def test_parse_branch_and_product_filters(self):
        """Test parsing branch and product filters"""
        request = self.factory.get('/', {
            'branch': 'branch-123',
            'product': 'product-456'
        })
        
        params = ReportFilterService.parse_filter_params(request)
        
        self.assertEqual(params.branch_id, 'branch-123')
        self.assertEqual(params.product_id, 'product-456')
    
    def test_parse_gender_filter(self):
        """Test parsing gender filter"""
        request = self.factory.get('/', {'gender': 'F'})
        
        params = ReportFilterService.parse_filter_params(request)
        
        self.assertEqual(params.gender, 'F')
    
    def test_parse_period_filter(self):
        """Test parsing period filter"""
        request = self.factory.get('/', {'period': 'monthly'})
        
        params = ReportFilterService.parse_filter_params(request)
        
        self.assertEqual(params.period, 'monthly')
    
    def test_default_period_is_all_time(self):
        """Test default period is 'all_time'"""
        request = self.factory.get('/')
        
        params = ReportFilterService.parse_filter_params(request)
        
        self.assertEqual(params.period, 'all_time')
    
    def test_parse_exclusion_flags(self):
        """Test parsing exclusion flags"""
        request = self.factory.get('/', {
            'exclude_rolled_over': 'false',
            'exclude_deleted': 'true'
        })
        
        params = ReportFilterService.parse_filter_params(request)
        
        self.assertFalse(params.exclude_rolled_over)
        self.assertTrue(params.exclude_deleted)
    
    def test_default_exclusion_flags_are_true(self):
        """Test default exclusion flags are True"""
        request = self.factory.get('/')
        
        params = ReportFilterService.parse_filter_params(request)
        
        self.assertTrue(params.exclude_rolled_over)
        self.assertTrue(params.exclude_deleted)
    
    def test_parse_empty_request(self):
        """Test parsing request with no parameters"""
        request = self.factory.get('/')
        
        params = ReportFilterService.parse_filter_params(request)
        
        self.assertIsNone(params.start_date)
        self.assertIsNone(params.end_date)
        self.assertIsNone(params.branch_id)
        self.assertIsNone(params.product_id)
        self.assertIsNone(params.gender)
        self.assertEqual(params.period, 'all_time')
        self.assertTrue(params.exclude_rolled_over)
        self.assertTrue(params.exclude_deleted)
