"""
Minimal tests for export service without database dependencies
"""

from decimal import Decimal
from datetime import date, datetime

# Test the export service methods directly
from reports.export_service import ReportExportService


def test_format_currency():
    """Test currency formatting"""
    service = ReportExportService()
    
    # Test with Decimal
    assert service.format_currency(Decimal('10000.50')) == 'KES 10,000.50'
    print("✓ Currency formatting with Decimal works")
    
    # Test with integer
    assert service.format_currency(5000) == 'KES 5,000.00'
    print("✓ Currency formatting with integer works")
    
    # Test with None
    assert service.format_currency(None) == 'KES 0.00'
    print("✓ Currency formatting with None works")
    
    # Test with float
    assert service.format_currency(1234.56) == 'KES 1,234.56'
    print("✓ Currency formatting with float works")


def test_format_date():
    """Test date formatting"""
    service = ReportExportService()
    
    # Test with date object
    test_date = date(2024, 1, 15)
    assert service.format_date(test_date) == '2024-01-15'
    print("✓ Date formatting with date object works")
    
    # Test with datetime object
    test_datetime = datetime(2024, 1, 15, 10, 30, 45)
    assert service.format_date(test_datetime) == '2024-01-15 10:30:45'
    print("✓ Date formatting with datetime object works")
    
    # Test with None
    assert service.format_date(None) == 'N/A'
    print("✓ Date formatting with None works")
    
    # Test with string
    assert service.format_date('2024-01-15') == '2024-01-15'
    print("✓ Date formatting with string works")


def test_export_service_initialization():
    """Test that export service initializes correctly"""
    service = ReportExportService()
    
    assert service.MAX_EXPORT_ROWS == 10000
    print("✓ Export service has correct MAX_EXPORT_ROWS")
    
    assert hasattr(service, 'pdf_styles')
    print("✓ Export service has pdf_styles")
    
    assert 'ReportTitle' in service.pdf_styles
    print("✓ Export service has custom ReportTitle style")
    
    assert 'ReportSubtitle' in service.pdf_styles
    print("✓ Export service has custom ReportSubtitle style")


def test_format_report_title():
    """Test report title formatting"""
    service = ReportExportService()
    
    assert service._format_report_title('loans_due') == 'Loans Due Report'
    print("✓ Report title formatting works for loans_due")
    
    assert service._format_report_title('processing_fees') == 'Processing Fees Report'
    print("✓ Report title formatting works for processing_fees")
    
    assert service._format_report_title('interest_income') == 'Interest Income Report'
    print("✓ Report title formatting works for interest_income")


def test_export_to_excel_basic():
    """Test basic Excel export without database"""
    service = ReportExportService()
    
    # Create minimal test data
    report_data = {
        'loans': [
            {
                'loan_number': 'LOAN-001',
                'borrower_name': 'Test User',
                'principal_amount': Decimal('10000'),
                'disbursement_date': date(2024, 1, 1)
            }
        ]
    }
    
    filters = {
        'start_date': date(2024, 1, 1),
        'end_date': date(2024, 1, 31)
    }
    
    response = service.export_to_excel(report_data, 'test_report', filters)
    
    # Check response properties
    assert response.status_code == 200
    print("✓ Excel export returns 200 status")
    
    assert 'spreadsheet' in response['Content-Type'].lower()
    print("✓ Excel export has correct content type")
    
    assert 'Content-Disposition' in response
    print("✓ Excel export has Content-Disposition header")
    
    assert 'attachment' in response['Content-Disposition'].lower()
    print("✓ Excel export Content-Disposition contains 'attachment'")
    
    assert len(response.content) > 0
    print("✓ Excel export has content")


def test_export_to_pdf_basic():
    """Test basic PDF export without database"""
    service = ReportExportService()
    
    # Create minimal test data
    report_data = {
        'loans': [
            {
                'loan_number': 'LOAN-001',
                'borrower_name': 'Test User',
                'principal_amount': Decimal('10000'),
                'disbursement_date': date(2024, 1, 1)
            }
        ]
    }
    
    filters = {
        'start_date': date(2024, 1, 1),
        'end_date': date(2024, 1, 31)
    }
    
    response = service.export_to_pdf(report_data, 'test_report', filters)
    
    # Check response properties
    assert response.status_code == 200
    print("✓ PDF export returns 200 status")
    
    assert response['Content-Type'] == 'application/pdf'
    print("✓ PDF export has correct content type")
    
    assert 'Content-Disposition' in response
    print("✓ PDF export has Content-Disposition header")
    
    assert 'attachment' in response['Content-Disposition'].lower()
    print("✓ PDF export Content-Disposition contains 'attachment'")
    
    assert len(response.content) > 0
    print("✓ PDF export has content")
    
    # Check that PDF starts with PDF magic bytes
    assert response.content[:4] == b'%PDF'
    print("✓ PDF export contains valid PDF header")


def test_export_with_empty_data():
    """Test exports with empty data"""
    service = ReportExportService()
    
    report_data = {'loans': []}
    filters = {}
    
    # Test Excel with empty data
    excel_response = service.export_to_excel(report_data, 'test_report', filters)
    assert excel_response.status_code == 200
    print("✓ Excel export handles empty data")
    
    # Test PDF with empty data
    pdf_response = service.export_to_pdf(report_data, 'test_report', filters)
    assert pdf_response.status_code == 200
    print("✓ PDF export handles empty data")


if __name__ == '__main__':
    print("\n=== Testing Export Service ===\n")
    
    try:
        test_format_currency()
        print()
        
        test_format_date()
        print()
        
        test_export_service_initialization()
        print()
        
        test_format_report_title()
        print()
        
        test_export_to_excel_basic()
        print()
        
        test_export_to_pdf_basic()
        print()
        
        test_export_with_empty_data()
        print()
        
        print("=" * 40)
        print("✅ All tests passed!")
        print("=" * 40)
        
    except AssertionError as e:
        print(f"\n❌ Test failed: {e}")
        import traceback
        traceback.print_exc()
    except Exception as e:
        print(f"\n❌ Error: {e}")
        import traceback
        traceback.print_exc()
