import time
from generate import generate_response
from plagCheck import is_generated_by_ai
from duplicateCheck import check_duplicate_content

MAX_RETRIES = 10

def rephrase_content(content):
    return "Rephrased: " + content

def regenerate_content_with_check(topic):
    attempt = 1
    last_content = None
    last_plag_report = None
    last_duplicate_report = None

    while attempt <= MAX_RETRIES:
        try:
            print(f"Attempt {attempt}: Regenerating content for topic: {topic}")
            generated_content = generate_response(topic)

            plag_report = is_generated_by_ai(generated_content)
            if not isinstance(plag_report, dict) or "is_ai_generated" not in plag_report:
                raise ValueError(f"Invalid plagiarism report format: {plag_report}")

            duplicate_report = check_duplicate_content(generated_content)
            duplicate_report = duplicate_report not in [None, False]

            print(f"Generated Content (Attempt {attempt}):\n{generated_content}")
            print(f"Plagiarism Report (Attempt {attempt}): {plag_report}")
            print(f"Duplicate Content Report (Attempt {attempt}): {duplicate_report}")

            if not plag_report["is_ai_generated"] and duplicate_report is False:
                print("Content is original and not duplicated.\n")
                return generated_content, plag_report, duplicate_report

            print(f"Content is {'AI generated' if plag_report['is_ai_generated'] else 'plagiarized'} or duplicated.")
            last_content = generated_content
            last_plag_report = plag_report
            last_duplicate_report = duplicate_report

            attempt += 1
            time.sleep(1)

        except Exception as e:
            print(f"Error during attempt {attempt}: {str(e)}")
            attempt += 1
            time.sleep(1)

    print("\nMaximum attempts reached. Attempting to rephrase the last generated content.")
    if last_content:
        try:
            rephrased_content = rephrase_content(last_content)

            plag_report = is_generated_by_ai(rephrased_content)
            duplicate_report = check_duplicate_content(rephrased_content)
            duplicate_report = duplicate_report not in [None, False]

            print(f"Rephrased Content:\n{rephrased_content}")
            print(f"Plagiarism Report (Rephrased): {plag_report}")
            print(f"Duplicate Content Report (Rephrased): {duplicate_report}")

            if not plag_report["is_ai_generated"] and duplicate_report is False:
                print("Rephrased content is original and not duplicated.\n")
                return rephrased_content, plag_report, duplicate_report

            return rephrased_content, plag_report, duplicate_report

        except Exception as e:
            print(f"Error during rephrasing: {str(e)}")
            return last_content, last_plag_report, last_duplicate_report

    print("No content to rephrase. Returning the last generated content.")
    return last_content, last_plag_report, last_duplicate_report
