#!/usr/bin/env python3
import requests
import urllib3
import re

# Disable SSL warnings
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

base_url = "https://localhost:443"
admin_user = {"username": "rac00n", "password": "1248163264"}

def test_detailed():
    session = requests.Session()
    
    # Login
    login_data = {
        "username": admin_user["username"],
        "password": admin_user["password"]
    }
    session.post(f"{base_url}/login", data=login_data, verify=False, allow_redirects=False)
    
    print("Testing License Page with Filters:")
    print("=" * 50)
    
    # Test 1: Basic license page
    response = session.get(f"{base_url}/licenses", verify=False)
    print(f"\n1. Basic licenses page - Status: {response.status_code}")
    
    # Check for filter dropdowns
    content = response.text
    if '<select' in content and 'filter' in content.lower():
        print("✓ Filter dropdowns found")
    else:
        print("✗ No filter dropdowns found")
    
    # Check for pagination
    if 'page=' in content or 'Seite' in content:
        print("✓ Pagination elements found")
    else:
        print("✗ No pagination elements")
    
    # Count licenses shown
    license_count = content.count('license_key') - 1  # Minus header
    print(f"Licenses shown: {license_count}")
    
    # Test 2: With type filter
    print("\n2. Testing type filter:")
    response = session.get(f"{base_url}/licenses?type=test", verify=False)
    print(f"Status: {response.status_code}")
    content = response.text
    test_licenses = content.count('PAGE-TEST-')
    print(f"Test licenses found: {test_licenses}")
    
    # Test 3: With status filter
    print("\n3. Testing status filter:")
    response = session.get(f"{base_url}/licenses?status=active", verify=False)
    print(f"Status: {response.status_code}")
    content = response.text
    active_count = content.count('aktiv') - content.count('inaktiv')
    print(f"Active status mentions: {active_count}")
    
    # Test 4: Check pagination info
    print("\n4. Checking total count display:")
    # Look for patterns like "X von Y Einträgen" or similar
    total_pattern = re.findall(r'(\d+)\s*von\s*(\d+)', content)
    if total_pattern:
        print(f"✓ Found pagination info: {total_pattern}")
    else:
        # Try other patterns
        if re.search(r'Gesamt.*\d+', content):
            print("✓ Found total count")
        elif 'total' in content.lower() and re.search(r'\d+', content):
            print("✓ Found total indicator")
        else:
            print("✗ No total count found")
    
    # Test 5: Customer page
    print("\n5. Testing customer page:")
    response = session.get(f"{base_url}/customers", verify=False)
    print(f"Status: {response.status_code}")
    content = response.text
    customer_count = content.count('customer_name') + content.count('<tr') - 2
    print(f"Customers shown: ~{customer_count}")
    
    # Print a snippet to see actual content
    print("\n6. HTML snippet from licenses page:")
    print("-" * 30)
    # Find filter section
    filter_start = content.find('filter')
    if filter_start > 0:
        print(content[max(0, filter_start-100):filter_start+200])
    else:
        print("No filter section found in HTML")

test_detailed()