import unicodedata
import re
import logging

import docx2txt

from gpt_processor import Translator


class DOCXProcessor:
    def __init__(self, file_path: str) -> None:
        self.file_path = file_path
        self.file_info = {
            "file_name": self.file_path.split("/")[-1],
            "file_format": "DOCX",
            "file_full_content": "",
        }
        self.__build_info()

    def __build_info(self) -> None:
        try:
            text = docx2txt.process(self.file_path)
            text = unicodedata.normalize("NFKD", text)
            text = text.replace("\n", " ").replace("\r", "")
            text = re.sub(" +", " ", text)
            self.file_info["is_chinese"] = self.__is_chinese(text)

            tranlator = Translator()
            self.file_info["file_full_content"] = (
                tranlator.translate_to_chinese(text)
                if not self.file_info["is_chinese"]
                else text
            )

        except FileNotFoundError:
            print(f"File not found: {self.file_path}")
        except Exception as e:
            print(f"An error occurred: {str(e)}")

    def __is_chinese(self, text: str) -> bool:
        for char in text:
            if char >= "\u4e00" and char <= "\u9fff":
                return True
        return False