import re
from remove_astricks import remove_ast
from typing import List

from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain.llms import OpenAI

from load_model import call_palm
from calling_apis import google_api_key, openai_api_key


def outlines_generator(idea : str, keywords : str, language='En', creativity='Original', model_name='Google Palm 2') -> str:
    
    '''
    Description:
    
        The outlines_generator() function  designed to generate an outline for an article based on a given idea and a set of keywords. 
        The function leverages an LLM model to create a structured outline with section headings. 
    '''
    
    '''
    Parameters:
    
        idea (str): The main idea or topic of the article for which you want to generate an outline.
        keywords (str): A set of keywords or topic-related terms that will be used as section headings in the outline. These headings help organize the content and provide a structure for the article.
        language (str): Opitonal Parameter -> The language of the model.
        creativity (str): Optional Parameter -> Controling the randomness of the model. Default value is Original
        model_name (str): Optional Parameter -> select the LLM model. Default Value is Google Palm 2
        
    '''
    
    '''
    Returns:
    
        outlines (str): The generated outline for the article, including section headings and placeholders for content under each heading.
    '''
    
    temp = 0

    if creativity == 'Original':
        temp = 0
    elif creativity == 'Balanced':
        temp = 0.25
    elif creativity == 'Creative':
        temp = 0.5
    elif creativity == 'Spirited':
        temp = 0.75
    elif creativity == 'Visionary':
        temp = 1


    if model_name == 'Google Palm 2':
        llm = call_palm(google_api_key, temperature=temp)

    elif model_name == 'GPT-3.5':
        llm = OpenAI(model_name='gpt-3.5-turbo', openai_api_key=openai_api_key, temperature=temp)

    elif model_name == 'GPT-4':
        llm = OpenAI(model_name='gpt-4', openai_api_key=openai_api_key, temperature=temp)


    if language == 'En':
        outlines_prompt = f"Generate an outline with at least 10 main points for an article on {idea}. Include key points related to {keywords}.\nBe creative and innovation in each main topic"

        outlines_promptTemp = PromptTemplate(
        input_variables=["text_input"],
        template="You are a professional writer\n\n{text_input}\n\nOutline (number each main point with roman numerals):")

    elif language == 'Ar':
        outlines_prompt = f"قم بتوليد مخطط تفصيلي يحتوي على ما لا يقل عن ١٠ نقاط رئيسية لمقال حول {idea}. وضمّن النقاط الرئيسية المتعلقة بـ {keywords}.\nكن مبدعًا ومبتكرًا في كل موضوع رئيسي."

        outlines_promptTemp = PromptTemplate(
        input_variables=["text_input"],
        template="أنت كاتب محترف\n\n{text_input}\n\nالخطوط العريضة (عد كل نقطة رئيسية بأرقام رومانية):")

    outlines_extraction_chain = LLMChain(llm=llm, prompt=outlines_promptTemp)
    outlines = outlines_extraction_chain.run(outlines_prompt)
    
    return outlines


def filtered_outlines(outline : str) -> List[str]:
    '''
    Description:
        This function processes an outline text by splitting it into sections based on Roman numeral patterns followed by a dot and space. It then generates a list of formatted sections.

    Parameters:
         `outline` -> Required: (str) The input outline text to be processed.

    Return:
        `sections`: (list) A list containing sections of the outline formatted with Arabic numerals (1, 2, 3, ...) and concatenated with their corresponding section content.
    '''
    
    sections = re.split(r'\b[IVXL]+\.\s', outline)[1:]
    sections = [f"{i}. {section}" for i, section in enumerate(sections, start=1)]

    return sections