# Copyright (c) CloudZero - ALL RIGHTS RESERVED - PROPRIETARY AND CONFIDENTIAL
# Unauthorized copying of this file and/or project, via any medium is strictly prohibited.
# Direct all questions to legal@cloudzero.com
#

import random
from typing import Any

# Functions for formatting CloudZero-specific data


def format_api_key_credentials(api_key: str) -> dict[str, str]:
    """
    Format API key credentials for use in a https request.

    Args:
        api_key: AWS API Key

    Returns:
        Dict with formatted credential for request headers
    """
    return {"x-api-key": api_key}


def format_reactor_status_response(
    details: dict[str, Any] | None = None, online_status: str = "", error: str = "", status_code: int = 0
) -> dict[str, Any]:
    """
    Format a reactor status response.

    Args:
        details: Detailed status information
        online_status: Online status string
        error: Error message if any
        status_code: HTTP status code

    Returns:
        Dict with formatted status response
    """
    if details is None:
        details = {}

    return {"details": details, "error": error, "status_code": status_code, "online_status": online_status}


def random_name() -> str:
    """
    Generate a random descriptive name using adjective-scientist format.

    Returns:
        String with randomly generated name
    """
    left = [
        "admiring",
        "adoring",
        "affectionate",
        "agitated",
        "amazing",
        "angry",
        "awesome",
        "blissful",
        "boring",
        "brave",
        "clever",
        "cocky",
        "compassionate",
        "competent",
        "condescending",
        "confident",
        "cranky",
        "dazzling",
        "determined",
        "distracted",
        "dreamy",
        "eager",
        "ecstatic",
        "elastic",
        "elated",
        "elegant",
        "eloquent",
        "epic",
        "fervent",
        "festive",
        "flamboyant",
        "focused",
        "friendly",
        "frosty",
        "gallant",
        "gifted",
        "goofy",
        "gracious",
        "happy",
        "hardcore",
        "heuristic",
        "hopeful",
        "hungry",
        "infallible",
        "inspiring",
        "jolly",
        "jovial",
        "keen",
        "kind",
        "laughing",
        "loving",
        "lucid",
        "mystifying",
        "modest",
        "musing",
        "naughty",
        "nervous",
        "nifty",
        "nostalgic",
        "objective",
        "optimistic",
        "peaceful",
        "pedantic",
        "pensive",
        "practical",
        "priceless",
        "quirky",
        "quizzical",
        "relaxed",
        "reverent",
        "romantic",
        "sad",
        "serene",
        "sharp",
        "silly",
        "sleepy",
        "stoic",
        "stupefied",
        "suspicious",
        "tender",
        "thirsty",
        "trusting",
        "unruffled",
        "upbeat",
        "vibrant",
        "vigilant",
        "vigorous",
        "wizardly",
        "wonderful",
        "xenodochial",
        "youthful",
        "zealous",
        "zen",
    ]

    right = [
        "albattani",
        "allen",
        "almeida",
        "agnesi",
        "archimedes",
        "ardinghelli",
        "aryabhata",
        "austin",
        "babbage",
        "banach",
        "bardeen",
        "bartik",
        "bassi",
        "beaver",
        "bell",
        "benz",
        "bhabha",
        "bhaskara",
        "blackwell",
        "bohr",
        "booth",
        "borg",
        "bose",
        "boyd",
        "brahmagupta",
        "brattain",
        "brown",
        "carson",
        "chandrasekhar",
        "shannon",
        "clarke",
        "colden",
        "cori",
        "cray",
        "curran",
        "curie",
        "darwin",
        "davinci",
        "dijkstra",
        "dubinsky",
        "easley",
        "edison",
        "einstein",
        "elion",
        "engelbart",
        "euclid",
        "euler",
        "fermat",
        "fermi",
        "feynman",
        "franklin",
        "galileo",
        "gates",
        "goldberg",
        "goldstine",
        "goldwasser",
        "golick",
        "goodall",
        "haibt",
        "hamilton",
        "hawking",
        "heisenberg",
        "hermann",
        "heyrovsky",
        "hodgkin",
        "hoover",
        "hopper",
        "hugle",
        "hypatia",
        "jackson",
        "jang",
        "jennings",
        "jepsen",
        "johnson",
        "joliot",
        "jones",
        "kalam",
        "kare",
        "keller",
        "kepler",
        "khorana",
        "kilby",
        "kirch",
        "knuth",
        "kowalevski",
        "lalande",
        "lamarr",
        "lamport",
        "leakey",
        "leavitt",
        "lewin",
        "lichterman",
        "liskov",
        "lovelace",
        "lumiere",
        "mahavira",
        "mayer",
        "mccarthy",
        "mcclintock",
        "mclean",
        "mcnulty",
        "meitner",
        "meninsky",
        "mestorf",
        "minsky",
        "mirzakhani",
        "morse",
        "murdock",
        "neumann",
        "newton",
        "nightingale",
        "nobel",
        "noether",
        "northcutt",
        "noyce",
        "panini",
        "pare",
        "pasteur",
        "payne",
        "perlman",
        "pike",
        "poincare",
        "poitras",
        "ptolemy",
        "raman",
        "ramanujan",
        "ride",
        "montalcini",
        "ritchie",
        "roentgen",
        "rosalind",
        "saha",
        "sammet",
        "shaw",
        "shirley",
        "shockley",
        "sinoussi",
        "snyder",
        "spence",
        "stallman",
        "stonebraker",
        "swanson",
        "swartz",
        "swirles",
        "tesla",
        "thompson",
        "torvalds",
        "turing",
        "varahamihira",
        "visvesvaraya",
        "volhard",
        "wescoff",
        "wiles",
        "williams",
        "wilson",
        "wing",
        "wozniak",
        "wright",
        "yalow",
        "yonath",
    ]

    # Safe random indexing to avoid index errors
    left_name = left[random.randint(0, len(left) - 1)]
    right_name = right[random.randint(0, len(right) - 1)]

    return f"{left_name}_{right_name}"


def datetime_to_utc_database_timeformat(datetime_input: Any) -> str:
    """
    Convert datetime object to a format for database import.

    Formats the datetime in a way that RedShift/Athena/Aurora can import automatically.
    See: https://docs.aws.amazon.com/redshift/latest/dg/automatic-recognition.html

    Args:
        datetime_input: A python datetime object

    Returns:
        String in format YYYY-MM-DD HH:MI:SS in UTC timezone
        If input is an unparseable string, returns the original string

    Examples:
        >>> from datetime import datetime, timezone
        >>> datetime_to_utc_database_timeformat(datetime(2009, 4, 19, 21, 12, 42, tzinfo=timezone.utc))
        '2009-04-19 21:12:42'
    """
    from src.utils.time import utc_datetime_from_anything

    try:
        dt = utc_datetime_from_anything(datetime_input)
        return dt.strftime("%Y-%m-%d %H:%M:%S")
    except ValueError:
        # Return original string for unparseable string inputs
        if isinstance(datetime_input, str):
            return datetime_input
        return ""
