import imghdr
import json
import re
import urllib
import zipfile

from bs4 import BeautifulSoup

from patch.include import *
from tqdm import tqdm


ERR_LINKS = set()
DONE = set()

def check_links(text: str) -> str:
  if not text:
    return text
  soup = BeautifulSoup(text, 'html.parser')
  for tag in soup.find_all('a'):
    if tag.attrs.get('cmf_converted'):
      continue
    if tag.attrs.get('href', '').startswith('https://anoirimoscow.atlassian.net') or tag.attrs.get('href', '').startswith('http://10.3.1.101'):
      print(tag)
      
      if '/wiki/' in tag.attrs['href']:
        ext_code = re.search(r"\/\d+", tag.attrs['href'])

        if not ext_code:
          ERR_LINKS.add(tag.attrs['href'])
          continue
        obj = models.CmfDocument.get(ext_id=ext_code.group(0)[1:], fields=['href'])
        if not obj:
          ERR_LINKS.add(tag.attrs['href'])
          continue
      elif '/browse/' in tag.attrs['href']:
        ext_code = re.search(r"\/\w+-\d+", tag.attrs['href'])
        print(ext_code)
        comment = models.CmfComment.get(filter=[['text', 'LIKE', f'%{ext_code.group(0)}%'], 
                                                ['cmf_import', '!=', None], ['log_level', '==', 2]], 
                                        fields=['parent.href', 'parent.import_raw_json'])
        if not comment:
          print('ERROR: Не нашли коммент')
          ERR_LINKS.add(tag.attrs['href'])
          continue
        if not comment.parent.import_raw_json:
          print('ERROR: задача без json_raw')
          ERR_LINKS.add(tag.attrs['href'])
          continue
        if not comment.parent.import_raw_json.value['key'] == ext_code.group(0)[1:]:
          ERR_LINKS.add(tag.attrs['href'])
          continue
        obj = comment.parent.value
      else:
        continue
      DONE.add(obj.code.value)
      task_tag = soup.new_tag('a')
      task_tag.attrs['href'] = obj.href
      task_tag.append(f"({obj.code.value})")
      print(f"task_tag={task_tag}")
      tag.attrs['cmf_converted'] = True
      tag.insert_after(task_tag)
  return str(soup)
      



@app_context(commit=True)
def patch():
    """
    Проходимся по документам с вложениями и добавляем списко вложений в текст документа если нету
    """
    _filter = [['text', '!=', None], ['import_raw_json', '!=', None]]
    tasks = models.CmfTask.list(filter=_filter, fields=['text', 'import_raw_json', 'comments.log_level', 
                                                        'comments.text'])
    for task in tqdm(tasks):
      print(task.import_raw_json.value['key'])
      task.text = check_links(task.text.value)
      if task.is_changed:
        task.save(only_data=True)
      for comment in task.comments:
        if comment.log_level == 1:
          continue
        comment.text = check_links(comment.text.value)
        if comment.is_changed:
          comment.save(only_data=True)
    for doc in tqdm(models.CmfDocument.list(filter=[['cmf_import', '!=', None], ['text', '!=', None]], 
                                            fields=['text', 'comments.log_level', 'comments.text'])):
      doc.text = check_links(doc.text.value)
      if doc.is_changed:
        doc.save(only_data=True)
      for comment in doc.comments:
        if comment.log_level == 1:
          continue
        comment.text = check_links(comment.text.value)
        if comment.is_changed:
          comment.save(only_data=True)
    print(f"Не удалось найти обьекты по ссылкам {ERR_LINKS}")
    print(f'Обработано {len(DONE)}')
      
      
    


if __name__ == "__main__":
    patch()
