import time
from datetime import datetime
from pytz import timezone
from pyzabbix import ZabbixAPI, ZabbixAPIException
from telebot import logger

from config import ZABBIX_URL, ZABBIX_API_TOKEN, ZABBIX_VERIFY_SSL, ZABBIX_TZ
from app.bot.utils.tg_escape_chars import escape_telegram_chars
TZ = timezone(ZABBIX_TZ)
verify_ssl = ZABBIX_VERIFY_SSL

def get_region_groups(region_id: str):
    """
    Получает список групп, имя которых содержит регион region_id, исключая 'test'.
    """
    try:
        zapi = ZabbixAPI(ZABBIX_URL)
        zapi.login(api_token=ZABBIX_API_TOKEN)
        zapi.session.verify = verify_ssl

        host_groups = zapi.hostgroup.get(output=["groupid", "name"], search={"name": region_id})
        filtered_groups = [group for group in host_groups if 'test' not in group['name'].lower()]
        return filtered_groups
    except Exception as e:
        logger.error(f"Error getting region groups for '{region_id}': {e}")
        return []

def get_all_groups_for_region(region_id: str):
    """
    Аналогично get_region_groups, получение всех групп по региону.
    """
    return get_region_groups(region_id)

def fetch_filtered_triggers(group_id):
    """
    Получение и фильтрация триггеров с severities 4 и 5,
    формирование HTML сообщений для отправки в Telegram.
    """
    try:
        zapi = ZabbixAPI(ZABBIX_URL)
        zapi.login(api_token=ZABBIX_API_TOKEN)
        zapi.session.verify = verify_ssl

        problems = zapi.problem.get(
            severities=[4, 5],
            suppressed=0,
            acknowledged=0,
            groupids=group_id
        )
        trigger_ids = [problem["objectid"] for problem in problems]

        if not trigger_ids:
            return []

        triggers = zapi.trigger.get(
            triggerids=trigger_ids,
            output=["triggerid", "description", "priority"],
            selectHosts=["hostid", "name"],
            monitored=1,
            expandDescription=1,
            expandComment=1,
            selectItems=["itemid", "lastvalue"],
            selectLastEvent=["clock", "eventid"]
        )

        events = zapi.event.get(
            severities=[4, 5],
            objectids=trigger_ids,
            select_alerts="mediatype"
        )

        pnet_mediatypes = {"Pnet integration JS 2025", "Pnet integration JS 2024", "Pnet integration new2"}

        pnet_triggers = []
        event_dict = {event["objectid"]: event for event in events}

        for trigger in triggers:
            event = event_dict.get(trigger["triggerid"])
            if event:
                for alert in event["alerts"]:
                    if alert["mediatypes"] and alert["mediatypes"][0]["name"] in pnet_mediatypes:
                        pnet_triggers.append(trigger)
                        break

        triggers_sorted = sorted(pnet_triggers, key=lambda t: int(t['lastEvent']['clock']))


        priority_map = {'4': 'HIGH', '5': 'DISASTER'}
        trigger_messages = []

        for trigger in triggers_sorted:
            event_time_epoch = int(trigger['lastEvent']['clock'])
            event_time = datetime.fromtimestamp(event_time_epoch, tz=TZ)
            description = escape_telegram_chars(trigger['description'])
            host = trigger['hosts'][0]['name']
            priority = priority_map.get(trigger['priority'], 'Неизвестно')
            item_ids = [item['itemid'] for item in trigger['items']]
            batchgraph_link = f"{ZABBIX_URL}/history.php?action=batchgraph&"
            batchgraph_link += "&".join([f"itemids[{item_id}]={item_id}" for item_id in item_ids])
            batchgraph_link += "&graphtype=0"
            description = description.replace("{HOST.NAME}", host)
            for i, item in enumerate(trigger['items']):
                lastvalue_placeholder = f"{{ITEM.LASTVALUE{i + 1}}}"
                if lastvalue_placeholder in description:
                    description = description.replace(lastvalue_placeholder, item['lastvalue'])
            event_time_formatted = event_time.strftime('%Y-%m-%d %H:%M:%S Мск')
            message = (f"<b>Host</b>: {host}\n"
                       f"<b>Описание</b>: {description}\n"
                       f"<b>Критичность</b>: {priority}\n"
                       f"<b>Время создания</b>: {event_time_formatted}\n"
                       f'<b>URL</b>: <a href="{batchgraph_link}">Ссылка на график</a>')
            trigger_messages.append(message)

        logger.info(f"Fetched {len(triggers_sorted)} triggers for group {group_id}.")
        return trigger_messages

    except Exception as e:
        logger.error(f"Error fetching triggers for group {group_id}: {e}")
        return []
