<?php
use Vanderbilt\REDCap\Classes\OpenAI\ChatGPTSummary;
use Vanderbilt\REDCap\Classes\OpenAI\Prompts;
use Vanderbilt\REDCap\Classes\GeminiAI\GeminiSummary;

if (isset($_GET['pid'])) {
    include_once dirname(dirname(__FILE__)) . '/Config/init_project.php';
} else {
    include_once dirname(dirname(__FILE__)) . '/Config/init_global.php';
    if (!ACCESS_CONTROL_CENTER) exit($lang['global_01']);
}
$output = ['status' => 0, 'message'   => ''];

if ($ai_services_enabled_global == '1') { // OpenAI Service
    $aiObj = new ChatGPTSummary();
} elseif ($ai_services_enabled_global == '2') { // GeminiAI service
    $aiObj = new GeminiSummary();
}
$data = '';
if (isset($_POST['action'])) {
    $param = $_POST['param'];

    switch ($_POST['action']) {
        case "check_grammer":
            $selectedResponse = $aiObj->generateResponse($_POST['content_str'], Prompts::PROMPT_CHECK_GRAMMAR, \AI::$callTypeWritingTools);
            $data = $selectedResponse['response'];
            break;

        case "fix_grammar":
            $selectedResponse = $aiObj->generateResponse($_POST['content_str'], Prompts::PROMPT_FIX_GRAMMAR, \AI::$callTypeWritingTools);
            $data = $selectedResponse['response'];
            break;

        case "get_reading_level":
        case "get_suggestion_reading_level":
            $selectedResponse = $aiObj->generateResponse($_POST['content_str'], Prompts::PROMPT_GET_READING_LEVEL, \AI::$callTypeWritingTools);
            $data = js_escape2($selectedResponse['response']);
            break;

        case "set_reading_level":
            if (in_array($param, ['5th', '6th', '7th'])) {
                $promptText = Prompts::PROMPT_SET_READING_LEVEL_PREFIX." ".$param.".\n[STRING]";
            } else if ($param == '8_9') {
                $promptText = Prompts::PROMPT_SET_READING_LEVEL_PREFIX." 8th & 9th grade.\n[STRING]";
            } else if ($param == '10_12') {
                $promptText = Prompts::PROMPT_SET_READING_LEVEL_PREFIX." 10th to 12th grade.\n[STRING]";
            } else if ($param == 'college') {
                $promptText = Prompts::PROMPT_SET_READING_LEVEL_PREFIX." College.\n[STRING]";
            } else if ($param == 'college_grad') {
                $promptText = Prompts::PROMPT_SET_READING_LEVEL_PREFIX." College graduate.\n[STRING]";
            } else if ($param == 'professional') {
                $promptText = Prompts::PROMPT_SET_READING_LEVEL_PREFIX." Professional.\n[STRING]";
            }

            $promptText .= Prompts::PROMPT_PLACEHOLDERS_TEXT;
            $selectedResponse = $aiObj->generateResponse($_POST['content_str'], $promptText, \AI::$callTypeWritingTools);
            $data = $selectedResponse['response'];
            break;
        case "set_length":
            if ($param == '25') {
                $promptText = Prompts::PROMPT_REDUCE_BY_25;
            } else if ($param == '50') {
                $promptText = Prompts::PROMPT_REDUCE_BY_50;
            } else if ($param == '75') {
                $promptText = Prompts::PROMPT_REDUCE_BY_75;
            } else if ($param == 'one_paragraph') {
                $promptText = Prompts::PROMPT_TO_ONE_PARAGRAPH;
            } else if ($param == '25+') {
                $promptText = Prompts::PROMPT_INCREASE_BY_25;
            } else if ($param == '50+') {
                $promptText = Prompts::PROMPT_INCREASE_BY_50;
            } else if ($param == '75+') {
                $promptText = Prompts::PROMPT_INCREASE_BY_75;
            }
            $promptText .= Prompts::PROMPT_PLACEHOLDERS_TEXT;
            $selectedResponse = $aiObj->generateResponse($_POST['content_str'], $promptText, \AI::$callTypeWritingTools);
            $data = $selectedResponse['response'];
            break;
        case "change_tone":
            if ($param == 'formal') {
                $promptText = Prompts::PROMPT_TONE_FORMAL;
            } else if ($param == 'friendly') {
                $promptText = Prompts::PROMPT_TONE_FRIENDLY;
            } else if ($param == 'encourage') {
                $promptText = Prompts::PROMPT_TONE_ENCOURAGE;
            } else if ($param == 'professional') {
                $promptText = Prompts::PROMPT_TONE_PROFESSIONAL;
            }
            $promptText .= Prompts::PROMPT_PLACEHOLDERS_TEXT;
            $selectedResponse = $aiObj->generateResponse($_POST['content_str'], $promptText, \AI::$callTypeWritingTools);
            $data = nl2br($selectedResponse['response']);
            break;

        case "custom_prompt":
            $selectedResponse = $aiObj->generateResponse('', $_POST['custom_prompt_str'], \AI::$callTypeWritingTools);
            $data = nl2br(htmlspecialchars($selectedResponse['response']));
            break;
    }

    $output = ['status' => 1, 'message' => $data];
    if (isset($selectedResponse['errors'])) {
        $output['errors'] = $selectedResponse['errors'];
    }
}

echo json_encode($output);