import httpx
from user_scanner.core.result import Result


async def _check(email: str) -> Result:
    url = "https://id.fox.com/status/v1/status"
    show_url = "https://foxnews.com"

    params = {
        'email': email
    }

    headers = {
        'User-Agent': "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Mobile Safari/537.36",
        'Accept': "*/*",
        'Accept-Encoding': "gzip, deflate, br, zstd",
        'x-api-key': "049f8b7844b84b9cb5f830f28f08648c",
        'origin': "https://auth.fox.com",
        'referer': "https://auth.fox.com/",
        'accept-language': "en-US,en;q=0.9"
    }

    try:
        async with httpx.AsyncClient(timeout=15.0) as client:
            response = await client.get(url, params=params, headers=headers)

            if response.status_code == 429:
                return Result.error("Rate limited wait for few minutes")

            if response.status_code != 200:
                return Result.error(f"HTTP Error: {response.status_code}")

            data = response.json()
            found = data.get("found")

            if found is True:
                return Result.taken(url=show_url)

            elif found is False:
                return Result.available(url=show_url)

            else:
                return Result.error("Unexpected response body structure, report it via GitHub issues")

    except httpx.ConnectTimeout:
        return Result.error("Connection timed out! maybe region blocks")
    except httpx.ReadTimeout:
        return Result.error("Server took too long to respond (Read Timeout)")
    except Exception as e:
        return Result.error(e)


async def validate_foxnews(email: str) -> Result:
    return await _check(email)
