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

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

# Login first
login_page = session.get(f"{base_url}/auth/login")
csrf_token = session.cookies.get('csrftoken')

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

login_response = session.post(f"{base_url}/auth/login", data=login_data)

if 'login' not in login_response.url:
    print("✅ Login successful!")
    
    # Test creating a channel via modal API
    channel_data = {
        'name': 'Test Channel via Modal',
        'display_name': 'TEST-MODAL',
        'channel_number': '999',
        'channel_type': 'satellite',
        'status': 'active',
        'description': 'This is a test channel created via modal API',
        'language': 'English',
        'category': 'Test',
        
    }
    
    # Get CSRF token for the API call
    csrf_token = session.cookies.get('csrftoken')
    
    response = session.post(
        f"{base_url}/channels/api/channels/create/",
        data=json.dumps(channel_data),
        headers={
            'Content-Type': 'application/json',
            'X-CSRFToken': csrf_token
        }
    )
    
    if response.status_code == 200:
        result = response.json()
        if result.get('success'):
            print("✅ Channel created successfully via modal API!")
            print(f"   📊 Channel ID: {result['channel']['id']}")
            print(f"   📊 Channel Name: {result['channel']['name']}")
            
            # Test getting the channel details
            channel_id = result['channel']['id']
            detail_response = session.get(f"{base_url}/channels/api/channels/{channel_id}/")
            
            if detail_response.status_code == 200:
                detail_data = detail_response.json()
                print("✅ Channel details retrieved successfully!")
                print(f"   📊 Full name: {detail_data['channel']['name']}")
                print(f"   📊 Status: {detail_data['channel']['status_display']}")
            else:
                print("❌ Failed to retrieve channel details")
        else:
            print(f"❌ Channel creation failed: {result.get('message')}")
    else:
        print(f"❌ API call failed: {response.status_code}")
        print(f"Response: {response.text}")
        
else:
    print("❌ Login failed")

