from typing import Optional


def is_service_name_compliant(service_name: str) -> bool:
    # TODO: use a regex
    return service_name and "_SVC" in service_name.upper()


def get_t_appid_from_service_name(service_name: str) -> str:
    if is_service_name_compliant(service_name):
        t_appid = service_name.split("_SVC")[-1]
        return f"SVC{t_appid}"
    return None


def dedup_entities(entities: list[dict]) -> list[dict]:
    entities_dict = {}
    for entity in entities:
        entities_dict[entity["id"]] = entity
    return list(entities_dict.values())


def build_resolved_service_dict(service, team_members: Optional[list[dict]] = None):
    if not is_service_name_compliant(service["name"]):
        return None

    t_appid = get_t_appid_from_service_name(service["name"])
    name = service["name"].split("_SVC")[0]

    return {
        "t_appid": t_appid,
        "id": service["id"],
        "name": name,
        "teams": service["teams"],
        "sre": team_members,
        "service": {
            "id": service["id"],
            "type": "service_reference",
            "summary": service["name"],
            "self": service["self"],
            "html_url": service["html_url"],
        },
    }
