from __future__ import annotations

from typing import Any, List


PRIMARY_AUX_PARENT_REGION_KEYS = {"east2", "qingdao"}
HARD_AUX_RANGE_REGION_KEYS = {"east2", "qingdao"}


def expected_public_ip_count(plan: Any) -> int:
    """按地区计划计算应创建或复用的公网 IP 数量。"""
    return (
        int(getattr(plan, "standard_nic_count", 0))
        + int(getattr(plan, "virtual_ip_count", 0))
        + int(getattr(plan, "aux_count", 0))
    )


def standard_private_ips(plan: Any) -> List[str]:
    """生成主网卡和标准网卡使用的内网 IP 列表。"""
    return [f"192.168.0.{10 + index}" for index in range(int(getattr(plan, "standard_nic_count", 0)))]


def aux_private_ips(plan: Any) -> List[str]:
    """生成辅助弹性网卡使用的内网 IP 列表。"""
    start = int(getattr(plan, "aux_start_octet", 0))
    return [f"192.168.0.{start + index}" for index in range(int(getattr(plan, "aux_count", 0)))]


def virtual_private_ips(plan: Any) -> List[str]:
    """生成虚拟 IP 使用的内网 IP 列表。"""
    start = int(getattr(plan, "virtual_ip_start_octet", 0))
    return [f"192.168.0.{start + index}" for index in range(int(getattr(plan, "virtual_ip_count", 0)))]


def parent_iface_for_index(index: int) -> str:
    """把网卡序号转换成 Linux 网卡名。"""
    return f"eth{index}"


def aux_should_use_primary_parent(plan: Any) -> bool:
    """判断辅助弹性网卡是否应该挂在主网卡父接口下。"""
    return bool(getattr(plan, "aux_parent_primary", False)) or str(getattr(plan, "key", "")) in PRIMARY_AUX_PARENT_REGION_KEYS


def aux_ip_range_is_hard(plan: Any) -> bool:
    """判断辅助弹性网卡 IP 段是否为地区硬性配置，禁止自动漂移到其它 IP。"""
    return str(getattr(plan, "key", "")) in HARD_AUX_RANGE_REGION_KEYS
