import argparse
import requests
from getpass import getpass

def main():
    parser = argparse.ArgumentParser(description="Erioon CLI")
    subparsers = parser.add_subparsers(dest='command')

    # Add login command
    subparsers.add_parser('login', help='Login to Erioon')

    args = parser.parse_args()

    if args.command == 'login':
        user_input = input("Email or API Key: ").strip()

        if user_input.startswith("erioon-sk"):
            # Login using API key
            payload = {
                "api_key": user_input,
                "email": None,
                "password": None
            }
        else:
            # Login using email + password
            password = getpass("Password: ")
            payload = {
                "api_key": None,
                "email": user_input,
                "password": password
            }

        headers = {
            "Content-Type": "application/json"
        }

        try:
            response = requests.post("https://sdk.erioon.com/login_sdk", json=payload, headers=headers)
            if response.status_code == 200:
                print("✅ Login successful.")
                # Optional: Store token here
            else:
                error = response.json().get("error", "Login failed.")
                print(f"❌ Login failed: {error}")
        except Exception as e:
            print(f"❌ Request failed: {str(e)}")
    else:
        parser.print_help()
