import sys
from pathlib import Path

ROOT_DIR = Path(__file__).resolve().parents[1]
for path in (ROOT_DIR / "api", ROOT_DIR / "conn"):
    if str(path) not in sys.path:
        sys.path.insert(0, str(path))

import phrase_similarity
import detailTicket
import filters
import tickets
import mysql5


HIGH_GRAVITY_WORDS = ("alta", "urgente", "critica", "critico", "bloccante", "grave", "high")


def _is_high_gravity(value):
    text = str(value or "").strip().lower()
    return any(word in text for word in HIGH_GRAVITY_WORDS)



def getTicketToday(teleId):
    
    listOfStrings = []
    listFilters = filters.getFilters(teleId)
    ticketAsString = ""
    for j in listFilters:

        docList = mysql5.getTicket(j[2], j[3])
        ticketAsString = ""
        for i in docList:
            ticketAsString += "🎫 /Ticket_dettaglio_"+ str(i[0]) + "\n" +"🏢 Cliente: "+ str(i[1]) + "\n"  +"🔎 Oggetto: "+ str(i[2])+ "\n"  +"🎨 Gravita': "+ str(i[3])+ "\n"  + "🧑 Competenza': "+ str(i[4])+ "\n" + "📅 Apertura': "+ str(i[6])  + "\n\n"     
        if len(docList):
            # max char 4096
           listOfStrings.append(str(ticketAsString))
    return listOfStrings



def getTicketTodayForNotification(teleId, high_gravity_only=False):
    
    listOfStrings = []
    listFilters = filters.getFilters(teleId)

    # print("Lista di filtri: ",listFilters)
    # print("\n\n\n")

    ticketTodayList = mysql5.getTicketsByFilterSet(listFilters)
    for i in ticketTodayList:
        if high_gravity_only and not _is_high_gravity(i[3]):
            continue

        ticket_id = int(i[0])
        if len(tickets.getTicketForTid(teleId, ticket_id)) > 0:
            continue

        object_ = str(i[2])
        if len(object_) > 250:
            object_ = object_[:250] + "..."
        ticketAsStringSingle = "🎫 /Ticket_dettaglio_"+ str(i[0]) + "\n https://tsnew.sanmarcoweb.com/it/ticket/index/index/operation/view/id/"+ str(i[0]) + "\n" +"🏢 Cliente: "+ str(i[1]) + "\n"  +"🔎 Oggetto: "+ str(object_) + "\n"  +"🎨 Gravita': "+ str(i[3])+ "\n"  + "🧑 Competenza: "+ str(i[4])+ "\n" + "📦 Prodotto:" + str(i[5])+ "\n" + "💜/preferito_"+ str(i[0])  + "\n\n"
        for j in listFilters:
            if len(tickets.getTickets(int(j[0]), ticket_id)) == 0:
                tickets.insertTicket(int(j[0]), ticket_id)
        listOfStrings.append(str(ticketAsStringSingle))
    # print("Lista ticket finale:: ",len(listOfStrings))
    # print("\n\n\n")
    return listOfStrings








def getDetailTicket(ticketId):
    downloaded = detailTicket.getDetail(str(ticketId))
    return downloaded





    

# newTicketList = getTicketTodayForNotification(145645559)

# for ticket in newTicketList:
#         hole_message = ticket
#         # print("Messaggio: ", ticket)
#                # Guessing from other ticket
#         prefix = "🎫 /Ticket_dettaglio_"
#         ticket_value = ticket.split(prefix, 1)[-1].split("\n", 1)[0].strip()
   
#         best_matches_indices = phrase_similarity.start_guessing_new(ticket_value)
#         for ticket_info in best_matches_indices:
#             # print("Gesing Ticket: ", str(ticket_info))
#             hole_message += str(ticket_info)

#         hole_message += "\n\n\n\n"
#         print(hole_message)
