#!/usr/bin/env python3
import requests
import sys

# Base URL
base_url = "http://173.212.199.208:8090"
session = requests.Session()

# Get login page to get CSRF token
login_page = session.get(f"{base_url}/auth/login")
print(f"Login page status: {login_page.status_code}")

# Extract CSRF token from cookies
csrf_token = session.cookies.get('csrftoken')
print(f"CSRF Token: {csrf_token}")

# Test login with updated credentials
login_data = {
    'email': 'admin@adtlas.com',
    'password': 'testpassword123',
    'csrfmiddlewaretoken': csrf_token
}

# Attempt login
login_response = session.post(f"{base_url}/auth/login", data=login_data)
print(f"Login response status: {login_response.status_code}")
print(f"Login response URL: {login_response.url}")

# Check if redirected to dashboard or still at login
if 'login' in login_response.url:
    print("Login failed - still at login page")
    # Print some of the response to debug
    print("Response text preview:", login_response.text[:500])
else:
    print("Login successful - redirected to:", login_response.url)
    
    # Now try to access channels page
    channels_response = session.get(f"{base_url}/channels/")
    print(f"Channels page status: {channels_response.status_code}")
    print(f"Channels page URL: {channels_response.url}")
    
    if channels_response.status_code == 200:
        print("SUCCESS: Channels page is accessible!")
        # Check if it contains expected content
        if 'channel' in channels_response.text.lower():
            print("Page contains channel-related content")
        else:
            print("Page doesn't seem to contain channel content")
    else:
        print(f"ERROR: Channels page returned status {channels_response.status_code}")

