import logging
from typing import Optional

from . import client
from .template import update_all_templates, initialize_and_bind_template, process_template_input
from .sg import discover_regions_from_api_with_cache


def t_main(action: str,
           security_rule_id: Optional[str]
           ) -> int:

    regions = client.load_regions_prefer_cache()
    if not regions:
        logging.error("[tencentcloud] No regions available to proceed with template action")
        return 1
    logging.info("[tencentcloud] Available regions: %d", len(regions))

    if action == 'init' and security_rule_id:
        logging.info("[tencentcloud] Target security rule ID: %s", security_rule_id)
        region_id = discover_regions_from_api_with_cache(regions, security_rule_id)
        if not region_id:
            logging.warning("[tencentcloud] Failed to discover region for security group '%s'; defaulting to first region in list", security_rule_id)
            return 2
        common_client = client.get_common_client(region_id, module="vpc", endpoint="vpc.tencentcloudapi.com")
    else:
        region_id = regions[0].get("RegionId") or regions[0].get("Region", "")
        if not region_id:
            logging.error("[tencentcloud] First region entry is missing 'RegionId' and 'Region' keys; cannot determine region to use")
            return 3
        logging.info("[tencentcloud] Using region '%s' for template operations", region_id)
        common_client = client.get_common_client(region_id, module="vpc", endpoint="vpc.tencentcloudapi.com")

    ACTION_MAP = {
        "init": lambda: initialize_and_bind_template(common_client, security_rule_id),
        "list": lambda: process_template_input(common_client),
        "set": lambda: update_all_templates(common_client),
    }

    if action in ACTION_MAP:
        return ACTION_MAP[action]()
    else:
        logging.error("[tencentcloud] Unsupported action: %s", action)
        return 4
