import string
import hashlib
import  random
import os
from PyPDF2 import PdfReader
from config import *
import re

FILE_NAME = 'manjil.pdf'
FILE_PATH = os.path.join(os.getcwd(), FILE_NAME)

def random_string_generator(string_length: int) -> str:
    letters = string.ascii_letters
    random_string =  ''.join(random.choice(letters) for i in range(string_length))
    return random_string

def hash_string(string_value: str) ->str:
    return hashlib.sha256(string_value.encode('utf-8')).hexdigest()

def read_pdf_human_readable(file_path: str) -> list[str]:
    pdf_page_text_contents: list = []
    reader: PdfReader = PdfReader(file_path)
    for i, page in enumerate(reader.pages):
        text: str = page.extract_text()
        if text:
            pdf_page_text_contents.append(text.strip())
    return pdf_page_text_contents

def is_valid_email(email):
    pattern = r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'
    if re.match(pattern, email):
        return True
    else:
        return False

def password_check_sanity(passwd: str) -> bool:
    if DISABLE_PASSWORD_SANITY_CHECKS:
        return True
    special_symbol_pattern = r'[$@#%]'
    upper_case_pattern = r'[A-Z]'
    lower_case_pattern = r'[a-z]'
    digit_pattern = r'[0-9]'

    if len(passwd) < 6:
        raise InsecurePasswordException('Password length should be at least 6 characters.')
    elif len(passwd) > 20:
        raise InsecurePasswordException('Password length should not be greater than 20 characters.')

    if not re.search(digit_pattern, passwd):
        raise InsecurePasswordException('Password should have at least one numeral.')

    if not re.search(upper_case_pattern, passwd):
        raise InsecurePasswordException('Password should have at least one uppercase letter.')

    if not re.search(lower_case_pattern, passwd):
        raise InsecurePasswordException('Password should have at least one lowercase letter.')

    if not re.search(special_symbol_pattern, passwd):
        raise InsecurePasswordException('Password should have at least one of the symbols $@#%.')

    return True


class InsecurePasswordException(Exception):
    pass
