"""
Hardcoded schema constants for topology validation.
These schemas define the structure and validation rules for network topology configuration.

DO NOT MODIFY: These schemas are hardcoded and not user-editable.
"""

# Tier 2 Topology Schema
TIER_2_XLS_SCHEMA = {
    "sheet_name": "2-Tier-Topology",
    "resource_list": [
        "Interfaces (ISL)",
        "Interfaces (Edge)",
        "Fabric",
        "Virtual Networks",
        "Toponodes",
    ],
    "pools": {
        "source_file": "utils/allocations-nodeinfra.yml",
        "enabled": True,
        "include": [
            "tunnel-index-pool",
            "asn-pool",
            "systemipv4-pool",
            "ipv4-pool",
            "vni-pool",
            "evi-pool",
            "vlan-pool",
            "es-index-pool",
            "lagid-pool",
            "lag-admin-key-pool",
            "loopback-id-pool",
            "ipv4-mgmt-pool",
        ],
    },
    "resources": {
        "Toponodes": {
            "required_columns": {
                "Network Element": {"type": "string", "required": True},
                "HW Type": {"type": "string"},
                "Labels": {"type": "string"},
                "Software Version": {"type": "string"},
            },
            "validation_rules": {
                "config": {"node_type_regex": "^(leaf|spine|superspine|borderleaf)"},
                "constraints": [
                    {
                        "name": "network_element_validation",
                        "type": "enum_validation",
                        "config": {
                            "column": "Network Element",
                            "patterns": [
                                "^leaf\\d+$",
                                "^spine\\d+$",
                                "^superspine\\d+$",
                                "^borderleaf\\d+$",
                            ],
                        },
                    },
                    {
                        "name": "unique_network_elements",
                        "type": "unique_column",
                        "config": {"column": "Network Element"},
                    },
                ],
                "grouping_constraints": [
                    {
                        "type": "node_type_grouping",
                        "groupby_column": "_node_type",
                        "constraints": [
                            {
                                "type": "required_for_type",
                                "config": {
                                    "node_type": "leaf",
                                    "required_columns": ["HW Type"],
                                    "allowed_values_for_columns": {
                                        "HW Type": [
                                            "7220 IXR-D2L",
                                            "7220 IXR-D3L",
                                            "7221 IXR-D2L",
                                            "7221 IXR-D3L",
                                            "7222 IXR-D2L",
                                            "7222 IXR-D3L",
                                        ]
                                    },
                                },
                            },
                            {
                                "type": "required_for_type",
                                "config": {
                                    "node_type": "spine",
                                    "required_columns": ["HW Type"],
                                    "allowed_values_for_columns": {
                                        "HW Type": [
                                            "7220 IXR-D2L",
                                            "7220 IXR-D3L",
                                            "7221 IXR-D2L",
                                            "7221 IXR-D3L",
                                            "7222 IXR-D2L",
                                            "7222 IXR-D3L",
                                        ]
                                    },
                                },
                            },
                            {
                                "type": "required_for_type",
                                "config": {
                                    "node_type": "borderleafleaf",
                                    "required_columns": ["HW Type"],
                                    "allowed_values_for_columns": {
                                        "HW Type": [
                                            "7220 IXR-D2L",
                                            "7220 IXR-D3L",
                                            "7221 IXR-D2L",
                                            "7221 IXR-D3L",
                                            "7222 IXR-D2L",
                                            "7222 IXR-D3L",
                                        ]
                                    },
                                },
                            },
                            {
                                "type": "required_for_type",
                                "config": {
                                    "node_type": "spine",
                                    "required_columns": ["HW Type"],
                                    "allowed_values_for_columns": {
                                        "HW Type": [
                                            "7220 IXR-D2L",
                                            "7220 IXR-D3L",
                                            "7221 IXR-D2L",
                                            "7221 IXR-D3L",
                                            "7222 IXR-D2L",
                                            "7222 IXR-D3L",
                                        ]
                                    },
                                },
                            },
                        ],
                    }
                ],
            },
        },
        "Fabric": {
            "required_columns": {
                "Fabric": {"type": "string", "required": True},
                "underlay protocol": {"type": "string", "required": True},
                "overlay protocol": {"type": "string", "required": True},
                "RR selector": {"type": "string", "required": True},
                "RR client selector": {"type": "string", "required": True},
                "Link Selector": {"type": "string", "required": True},
                "Node Selectors": {"type": "string", "required": True},
                "Fabric Selectors": {"type": "string", "required": False},
            },
            "validation_rules": {
                "constraints": [
                    {
                        "name": "underlay_protocol_validation",
                        "type": "enum_validation",
                        "config": {
                            "column": "underlay protocol",
                            "allowed_values": ["ebgp"],
                        },
                    },
                    {
                        "name": "overlay_protocol_validation",
                        "type": "enum_validation",
                        "config": {
                            "column": "overlay protocol",
                            "allowed_values": ["ibgp"],
                        },
                    },
                ]
            },
        },
        "Interfaces (ISL)": {
            "required_columns": {
                "Network Element A": {"type": "string", "required": True},
                "Network Element B": {"type": "string", "required": True},
                "Port on Device A": {"type": "string", "required": True},
                "Port on Device B": {"type": "string", "required": True},
                "Ethernet Speed": {"type": "string", "required": True},
                "Encapsulation": {"type": "string", "required": False},
                "Labels": {"type": "string", "required": False},
            },
            "topo_link_validation_rules": {
                "config": {
                    "device_port_mapping": {
                        "device_a_col": "Network Element A",
                        "device_b_col": "Network Element B",
                        "port_a_col": "Port on Device A",
                        "port_b_col": "Port on Device B",
                    },
                    "node_type_regex": "^(leaf|spine|superspine|borderleaf)",
                },
                "constraints": [
                    {
                        "name": "network_element_a_validation (topolink)",
                        "type": "enum_validation",
                        "config": {
                            "column": "Network Element A",
                            "patterns": [
                                "^leaf\\d+$",
                                "^spine\\d+$",
                                "^superspine\\d+$",
                                "^borderleaf\\d+$",
                            ],
                        },
                    },
                    {
                        "name": "network_element_b_validation (topolink)",
                        "type": "enum_validation",
                        "config": {
                            "column": "Network Element B",
                            "patterns": [
                                "^leaf\\d+$",
                                "^spine\\d+$",
                                "^superspine\\d+$",
                                "^borderleaf\\d+$",
                            ],
                        },
                    },
                    {
                        "name": "allowed_connections (topolink)",
                        "type": "allowed_pairs",
                        "config": {
                            "pairs": [
                                ["leaf", "spine"],
                                ["spine", "borderleaf"],
                                ["spine", "superspine"],
                                ["superspine", "borderleaf"],
                            ]
                        },
                    },
                    {
                        "name": "unique_device_port_combinations (topolink)",
                        "type": "column_mapping",
                        "config": {
                            "device_a_col": "Network Element A",
                            "device_b_col": "Network Element B",
                            "port_a_col": "Port on Device A",
                            "port_b_col": "Port on Device B",
                        },
                    },
                ],
            },
            "interface_validation_rules": {
                "constraints": [
                    {
                        "name": "encapsulation_validation (iface)",
                        "type": "enum_validation",
                        "config": {
                            "column": "Encapsulation",
                            "allowed_values": ["null", "dot1q"],
                        },
                    },
                ]
            },
        },
        "Interfaces (Edge)": {
            "required_columns": {
                "Network Element": {"type": "string", "required": True},
                "Port on Device": {"type": "string", "required": True},
                "Type": {"type": "string", "required": True},
                "Ethernet Speed": {"type": "string", "required": True},
                "Encapsulation": {"type": "string", "required": False},
                "Multihoming type": {"type": "string", "required": False},
                "Labels": {"type": "string", "required": False},
                "Client": {"type": "string", "required": False},
            },
            "validation_rules": {
                "config": {
                    "device_port_mapping": {
                        "device_col": "Network Element",
                        "port_col": "Port on Device",
                    }
                },
                "constraints": [
                    {
                        "name": "network_element_validation",
                        "type": "enum_validation",
                        "config": {
                            "column": "Network Element",
                            "patterns": ["^leaf\\d+$"],
                        },
                    },
                    {
                        "name": "encapsulation_validation",
                        "type": "enum_validation",
                        "config": {
                            "column": "Encapsulation",
                            "allowed_values": ["null", "dot1q"],
                        },
                    },
                    {
                        "name": "unique_device_port",
                        "type": "column_mapping",
                        "config": {
                            "device_col": "Network Element",
                            "port_col": "Port on Device",
                        },
                    },
                ],
                "grouping_constraints": [
                    {
                        "type": "lag_grouping",
                        "groupby_column": "Type",
                        "lag_filter": {"column": "Type", "identifier": "lag"},
                        "constraints": [
                            {
                                "name": "min_lag_members",
                                "type": "min_members",
                                "config": {"min_lag_members": 2},
                            },
                            {
                                "name": "lag_multihoming_required",
                                "type": "required_for_type",
                                "config": {
                                    "required_columns": ["Multihoming type"],
                                    "allowed_values_for_columns": {
                                        "Multihoming type": [
                                            "Active/Active",
                                            "Active/Stand By",
                                            "Port Active",
                                        ]
                                    },
                                },
                            },
                            {
                                "name": "consistent_multihoming_type",
                                "type": "consistent_type",
                                "config": {"column": "Multihoming type"},
                            },
                        ],
                    }
                ],
            },
        },
        "Virtual Networks": {
            "required_columns": {
                "VNET": {"type": "string", "required": True},
                "Bridge Domain": {"type": "string", "required": True},
                "Vlan id": {"type": "int", "required": True},
                "interfaceSelector": {"type": "string", "required": False},
                "Router": {"type": "string", "required": False},
                "IRB ipv4": {"type": "ipv4", "required": False},
            },
            "validation_rules": {
                "config": {
                    "vnet_context": {
                        "vnet_column": "VNET",
                        "vnet_id_column": "_vnet_id",
                        "vnet_type_column": "_vnet_type",
                    }
                },
                "constraints": [
                    {
                        "name": "vnet_type_validation",
                        "type": "enum_validation",
                        "config": {
                            "column": "VNET",
                            "allowed_values": ["layer2", "layer3"],
                        },
                    }
                ],
                "grouping_constraints": [
                    {
                        "groupby_column": "_vnet_id",
                        "constraints": [
                            {
                                "name": "layer3_required_fields",
                                "type": "required_for_type",
                                "config": {
                                    "vnet_type": "layer3",
                                    "required_columns": ["Router", "IRB ipv4"],
                                    "allowed_values_for_columns": {
                                        "Router": ["router"]
                                    },
                                },
                            },
                            {
                                "name": "layer3_ip_validation",
                                "type": "ip_address",
                                "config": {
                                    "vnet_type": "layer3",
                                    "column": "IRB ipv4",
                                    "required": True,
                                    "unique_per_group": True,
                                },
                            },
                            {
                                "name": "unique_values_per_vnet",
                                "type": "unique_per_group",
                                "config": {
                                    "unique_columns": ["Bridge Domain", "Vlan id"]
                                },
                            },
                        ],
                    }
                ],
            },
        },
    },
}

# Tier 3 Topology Schema (identical to Tier 2 for now)
TIER_3_XLS_SCHEMA = {
    "sheet_name": "3-Tier-Topology",
    "resource_list": [
        "Interfaces (ISL)",
        "Interfaces (Edge)",
        "Fabric",
        "Virtual Networks",
        "Toponodes",
    ],
    "pools": {
        "source_file": "utils/allocations-nodeinfra.yml",
        "enabled": True,
        "include": [
            "tunnel-index-pool",
            "asn-pool",
            "systemipv4-pool",
            "ipv4-pool",
            "vni-pool",
            "evi-pool",
            "vlan-pool",
            "es-index-pool",
            "lagid-pool",
            "lag-admin-key-pool",
            "loopback-id-pool",
            "ipv4-mgmt-pool",
        ],
    },
    "resources": {
        "Toponodes": {
            "required_columns": {
                "Network Element": {"type": "string", "required": True},
                "HW Type": {"type": "string"},
                "Labels": {"type": "string"},
                "Software Version": {"type": "string"},
            },
            "validation_rules": {
                "config": {"node_type_regex": "^(leaf|spine|superspine|borderleaf)"},
                "constraints": [
                    {
                        "name": "network_element_validation",
                        "type": "enum_validation",
                        "config": {
                            "column": "Network Element",
                            "patterns": [
                                "^leaf\\d+$",
                                "^spine\\d+$",
                                "^superspine\\d+$",
                                "^borderleaf\\d+$",
                            ],
                        },
                    },
                    {
                        "name": "unique_network_elements",
                        "type": "unique_column",
                        "config": {"column": "Network Element"},
                    },
                ],
                "grouping_constraints": [
                    {
                        "type": "node_type_grouping",
                        "groupby_column": "_node_type",
                        "constraints": [
                            {
                                "type": "required_for_type",
                                "config": {
                                    "node_type": "leaf",
                                    "required_columns": ["HW Type"],
                                    "allowed_values_for_columns": {
                                        "HW Type": [
                                            "7220 IXR-D3L",
                                            "7221 IXR-D3L",
                                            "7222 IXR-D3L",
                                            "7221 IXR-D2L",
                                            "7222 IXR-D2L",
                                            "7220 IXR-D2L",
                                        ]
                                    },
                                },
                            },
                            {
                                "type": "required_for_type",
                                "config": {
                                    "node_type": "spine",
                                    "required_columns": ["HW Type"],
                                    "allowed_values_for_columns": {
                                        "HW Type": [
                                            "7220 IXR-D3L",
                                            "7221 IXR-D3L",
                                            "7222 IXR-D3L",
                                        ]
                                    },
                                },
                            },
                            {
                                "type": "required_for_type",
                                "config": {
                                    "node_type": "borderleafleaf",
                                    "required_columns": ["HW Type"],
                                    "allowed_values_for_columns": {
                                        "HW Type": [
                                            "7220 IXR-D3L",
                                            "7221 IXR-D3L",
                                            "7222 IXR-D3L",
                                        ]
                                    },
                                },
                            },
                            {
                                "type": "required_for_type",
                                "config": {
                                    "node_type": "spine",
                                    "required_columns": ["HW Type"],
                                    "allowed_values_for_columns": {
                                        "HW Type": [
                                            "7220 IXR-D3L",
                                            "7221 IXR-D3L",
                                            "7222 IXR-D3L",
                                        ]
                                    },
                                },
                            },
                        ],
                    }
                ],
            },
        },
        "Fabric": {
            "required_columns": {
                "Fabric": {"type": "string", "required": True},
                "underlay protocol": {"type": "string", "required": True},
                "overlay protocol": {"type": "string", "required": True},
                "RR selector": {"type": "string", "required": True},
                "RR client selector": {"type": "string", "required": True},
                "Link Selector": {"type": "string", "required": True},
                "Node Selectors": {"type": "string", "required": True},
                "Fabric Selectors": {"type": "string", "required": False},
            },
            "validation_rules": {
                "constraints": [
                    {
                        "name": "underlay_protocol_validation",
                        "type": "enum_validation",
                        "config": {
                            "column": "underlay protocol",
                            "allowed_values": ["ebgp"],
                        },
                    },
                    {
                        "name": "overlay_protocol_validation",
                        "type": "enum_validation",
                        "config": {
                            "column": "overlay protocol",
                            "allowed_values": ["ibgp"],
                        },
                    },
                ]
            },
        },
        "Interfaces (ISL)": {
            "required_columns": {
                "Network Element A": {"type": "string", "required": True},
                "Network Element B": {"type": "string", "required": True},
                "Port on Device A": {"type": "string", "required": True},
                "Port on Device B": {"type": "string", "required": True},
                "Ethernet Speed": {"type": "string", "required": True},
                "Encapsulation": {"type": "string", "required": False},
                "Labels": {"type": "string", "required": False},
            },
            "topo_link_validation_rules": {
                "config": {
                    "device_port_mapping": {
                        "device_a_col": "Network Element A",
                        "device_b_col": "Network Element B",
                        "port_a_col": "Port on Device A",
                        "port_b_col": "Port on Device B",
                    },
                    "node_type_regex": "^(leaf|spine|superspine|borderleaf)",
                },
                "constraints": [
                    {
                        "name": "network_element_a_validation (topolink)",
                        "type": "enum_validation",
                        "config": {
                            "column": "Network Element A",
                            "patterns": [
                                "^leaf\\d+$",
                                "^spine\\d+$",
                                "^superspine\\d+$",
                                "^borderleaf\\d+$",
                            ],
                        },
                    },
                    {
                        "name": "network_element_b_validation (topolink)",
                        "type": "enum_validation",
                        "config": {
                            "column": "Network Element B",
                            "patterns": [
                                "^leaf\\d+$",
                                "^spine\\d+$",
                                "^superspine\\d+$",
                                "^borderleaf\\d+$",
                            ],
                        },
                    },
                    {
                        "name": "allowed_connections (topolink)",
                        "type": "allowed_pairs",
                        "config": {
                            "pairs": [
                                ["leaf", "spine"],
                                ["spine", "borderleaf"],
                                ["spine", "superspine"],
                                ["superspine", "borderleaf"],
                            ]
                        },
                    },
                    {
                        "name": "unique_device_port_combinations (topolink)",
                        "type": "column_mapping",
                        "config": {
                            "device_a_col": "Network Element A",
                            "device_b_col": "Network Element B",
                            "port_a_col": "Port on Device A",
                            "port_b_col": "Port on Device B",
                        },
                    },
                ],
            },
            "interface_validation_rules": {
                "constraints": [
                    {
                        "name": "encapsulation_validation (iface)",
                        "type": "enum_validation",
                        "config": {
                            "column": "Encapsulation",
                            "allowed_values": ["null", "dot1q"],
                        },
                    },
                ]
            },
        },
        "Interfaces (Edge)": {
            "required_columns": {
                "Network Element": {"type": "string", "required": True},
                "Port on Device": {"type": "string", "required": True},
                "Type": {"type": "string", "required": True},
                "Ethernet Speed": {"type": "string", "required": True},
                "Encapsulation": {"type": "string", "required": False},
                "Multihoming type": {"type": "string", "required": False},
                "Labels": {"type": "string", "required": False},
                "Client": {"type": "string", "required": False},
            },
            "validation_rules": {
                "config": {
                    "device_port_mapping": {
                        "device_col": "Network Element",
                        "port_col": "Port on Device",
                    }
                },
                "constraints": [
                    {
                        "name": "network_element_validation",
                        "type": "enum_validation",
                        "config": {
                            "column": "Network Element",
                            "patterns": ["^leaf\\d+$"],
                        },
                    },
                    {
                        "name": "encapsulation_validation",
                        "type": "enum_validation",
                        "config": {
                            "column": "Encapsulation",
                            "allowed_values": ["null", "dot1q"],
                        },
                    },
                    {
                        "name": "unique_device_port",
                        "type": "column_mapping",
                        "config": {
                            "device_col": "Network Element",
                            "port_col": "Port on Device",
                        },
                    },
                ],
                "grouping_constraints": [
                    {
                        "type": "lag_grouping",
                        "groupby_column": "Type",
                        "lag_filter": {"column": "Type", "identifier": "lag"},
                        "constraints": [
                            {
                                "name": "min_lag_members",
                                "type": "min_members",
                                "config": {"min_lag_members": 2},
                            },
                            {
                                "name": "lag_multihoming_required",
                                "type": "required_for_type",
                                "config": {
                                    "required_columns": ["Multihoming type"],
                                    "allowed_values_for_columns": {
                                        "Multihoming type": [
                                            "Active/Active",
                                            "Active/Stand By",
                                            "Port Active",
                                        ]
                                    },
                                },
                            },
                            {
                                "name": "consistent_multihoming_type",
                                "type": "consistent_type",
                                "config": {"column": "Multihoming type"},
                            },
                        ],
                    }
                ],
            },
        },
        "Virtual Networks": {
            "required_columns": {
                "VNET": {"type": "string", "required": True},
                "Bridge Domain": {"type": "string", "required": True},
                "Vlan id": {"type": "int", "required": True},
                "interfaceSelector": {"type": "string", "required": False},
                "Router": {"type": "string", "required": False},
                "IRB ipv4": {"type": "ipv4", "required": False},
            },
            "validation_rules": {
                "config": {
                    "vnet_context": {
                        "vnet_column": "VNET",
                        "vnet_id_column": "_vnet_id",
                        "vnet_type_column": "_vnet_type",
                    }
                },
                "constraints": [
                    {
                        "name": "vnet_type_validation",
                        "type": "enum_validation",
                        "config": {
                            "column": "VNET",
                            "allowed_values": ["layer2", "layer3"],
                        },
                    }
                ],
                "grouping_constraints": [
                    {
                        "groupby_column": "_vnet_id",
                        "constraints": [
                            {
                                "name": "layer3_required_fields",
                                "type": "required_for_type",
                                "config": {
                                    "vnet_type": "layer3",
                                    "required_columns": ["Router", "IRB ipv4"],
                                    "allowed_values_for_columns": {
                                        "Router": ["router"]
                                    },
                                },
                            },
                            {
                                "name": "layer3_ip_validation",
                                "type": "ip_address",
                                "config": {
                                    "vnet_type": "layer3",
                                    "column": "IRB ipv4",
                                    "required": True,
                                    "unique_per_group": True,
                                },
                            },
                            {
                                "name": "unique_values_per_vnet",
                                "type": "unique_per_group",
                                "config": {
                                    "unique_columns": ["Bridge Domain", "Vlan id"]
                                },
                            },
                        ],
                    }
                ],
            },
        },
    },
}

# Tier 4 Topology Schema (placeholder - add actual tier 4 schema if different)
TIER_4_XLS_SCHEMA = TIER_2_XLS_SCHEMA.copy()
TIER_4_XLS_SCHEMA["sheet_name"] = "2-Tier-Topology"

# Tier schema mapping
TIER_SCHEMA_MAPPING = {
    "tier_to_schema_mapping": {
        "2-tier": "tier_2_xls_schema.yaml",
        "3-tier": "tier_3_xls_schema.yaml",
        "4-tier": "tier_4_xls_schema.yaml",
    },
    "fabric_topology_config": {
        "parameters_marker": "Parameter",
        "parameters": {
            "tier": {"row_name": "Tier", "column_name": "site#1"},
            "namespace": {"row_name": "Tier", "column_name": "Namespace"},
            "num_pods": {"row_name": "Number of POD", "column_name": "site#1"},
        },
    },
}


def get_schema_by_tier(tier: str) -> dict:
    """
    Get the schema dictionary for a given tier.

    Args:
        tier: Tier identifier (e.g., "2-tier", "3-tier", "4-tier")

    Returns:
        Schema dictionary

    Raises:
        ValueError: If tier is not recognized
    """
    tier_lower = tier.lower()

    if tier_lower == "2-tier":
        return TIER_2_XLS_SCHEMA
    elif tier_lower == "3-tier":
        return TIER_3_XLS_SCHEMA
    elif tier_lower == "4-tier":
        return TIER_4_XLS_SCHEMA
    else:
        raise ValueError(
            f"Unknown tier: {tier}. Expected '2-tier', '3-tier', or '4-tier'"
        )
