import requests
from bs4 import BeautifulSoup

print("Testing Dashboard Access")
print("=" * 60)

# Create session and login
session = requests.Session()
response = session.get("http://173.212.199.208:8090/auth/login")
soup = BeautifulSoup(response.text, 'html.parser')
csrf_token = soup.find('input', {'name': 'csrfmiddlewaretoken'})['value']

login_data = {
    'csrfmiddlewaretoken': csrf_token,
    'email': 'admin@adtlas.com',
    'password': 'Admin@123456'
}

response = session.post(
    "http://173.212.199.208:8090/auth/login",
    data=login_data,
    headers={'Referer': 'http://173.212.199.208:8090/auth/login'},
    allow_redirects=False
)

print(f"1. Login status: {response.status_code}")
if response.status_code == 302:
    print(f"   ✓ Login successful, redirecting to: {response.headers.get('Location')}")

# Now try to access dashboard (without trailing slash)
print("\n2. Accessing dashboard...")
dashboard_response = session.get("http://173.212.199.208:8090/dashboard", allow_redirects=True)
print(f"   Status: {dashboard_response.status_code}")
print(f"   Final URL: {dashboard_response.url}")

if dashboard_response.status_code == 200:
    print("   ✓ Dashboard loaded successfully!")
    # Check if we're actually on the dashboard
    if 'Dashboard' in dashboard_response.text or 'dashboard' in dashboard_response.text.lower():
        print("   ✓ Confirmed: Dashboard content found")
    else:
        print("   ⚠ Warning: Page loaded but doesn't appear to be the dashboard")
elif dashboard_response.status_code == 500:
    print("   ✗ Server error on dashboard")
    # Try to get error details
    if 'NoReverseMatch' in dashboard_response.text:
        print("   Error: Template has invalid URL references")
else:
    print(f"   ✗ Unexpected status: {dashboard_response.status_code}")
