nhance/app/Controllers/Chatbot/medicalClaimFormConversations.php
2025-02-17 09:20:43 +05:30

317 lines
12 KiB
PHP

<?php
namespace App\Controllers\Chatbot;
use App\Helpers\ChatbotHelper;
use BotMan\BotMan\Messages\Outgoing\Question;
use BotMan\BotMan\Messages\Outgoing\Actions\Button;
use BotMan\BotMan\Messages\Conversations\Conversation;
class medicalClaimFormConversations extends Conversation
{
private $currentMemberData = [
'name' => null,
'dob' => null,
'si' => null,
'pre_existing_disease' => null
];
public function run()
{
// Initialize state if not exists
if (!$this->bot->userStorage()->get('claim_form_state')) {
$this->bot->userStorage()->save([
'claim_form_state' => [
'dependency_count' => 0,
'current_member' => 'primary',
'dependencies' => [],
'primary_insurer' => null
]
]);
}
$state = $this->bot->userStorage()->get('claim_form_state');
$memberType = ($state['current_member'] === 'primary') ? 'primary insured person' : 'dependency';
$this->askName($memberType);
}
private function askName($memberType)
{
$this->ask("Enter the Name of the $memberType?", function($answer) {
$name = $answer->getText();
if (empty($name)) {
$this->say('Name Cannot be Empty');
return $this->run();
}
// Get existing state and update only the name
$state = $this->bot->userStorage()->get('claim_form_state');
if ($state['current_member'] === 'primary') {
if (!isset($state['primary_insurer'])) {
$state['primary_insurer'] = [];
}
$state['primary_insurer']['name'] = $name;
} else {
if (!isset($state['temp_dependency'])) {
$state['temp_dependency'] = [];
}
$state['temp_dependency']['name'] = $name;
}
$this->bot->userStorage()->save(['claim_form_state' => $state]);
$this->askDOB();
});
}
private function askDOB()
{
$this->ask('Enter Date of Birth (DD/MM/YYYY)?', function($answer) {
$dob = $answer->getText();
if (empty($dob)) {
$this->say('Date of Birth Cannot be Empty');
return $this->askDOB();
}
// Get existing state and update only the DOB
$state = $this->bot->userStorage()->get('claim_form_state');
if ($state['current_member'] === 'primary') {
$state['primary_insurer']['dob'] = $dob;
} else {
$state['temp_dependency']['dob'] = $dob;
}
$this->bot->userStorage()->save(['claim_form_state' => $state]);
$this->askSumInsured();
});
}
private function askSumInsured()
{
$this->ask('Enter Sum Insured Amount?', function($answer) {
$sum_insured = $answer->getText();
if (empty($sum_insured)) {
$this->say('Sum Insured Cannot be Empty');
return $this->askSumInsured();
}
// Get existing state and update only the sum insured
$state = $this->bot->userStorage()->get('claim_form_state');
if ($state['current_member'] === 'primary') {
$state['primary_insurer']['si'] = $sum_insured;
} else {
$state['temp_dependency']['si'] = $sum_insured;
}
$this->bot->userStorage()->save(['claim_form_state' => $state]);
$this->askPreExistingDisease();
});
}
private function askPreExistingDisease()
{
$this->ask('Enter Pre-Existing Disease if any (type "none" if none)?', function($answer) {
$pre_existing_disease = $answer->getText();
if (empty($pre_existing_disease)) {
$this->say('Please enter none if no pre-existing conditions');
return $this->askPreExistingDisease();
}
// Get existing state and update the disease info
$state = $this->bot->userStorage()->get('claim_form_state');
if ($state['current_member'] === 'primary') {
$state['primary_insurer']['pre_existing_disease'] = $pre_existing_disease;
$state['primary_insurer']['type'] = 'primary';
} else {
$state['temp_dependency']['pre_existing_disease'] = $pre_existing_disease;
$state['temp_dependency']['type'] = 'dependency';
// Move completed dependency to dependencies array
if (!isset($state['dependencies'])) {
$state['dependencies'] = [];
}
$state['dependencies'][] = $state['temp_dependency'];
$state['temp_dependency'] = null; // Clear temporary dependency
}
$this->bot->userStorage()->save(['claim_form_state' => $state]);
// For debugging
log_message('debug', 'Final state after all data collection: ' . json_encode($state));
$this->saveMemberAndContinue();
});
}
private function saveMemberAndContinue()
{
$state = $this->bot->userStorage()->get('claim_form_state');
$memberData = $state['current_member'] === 'primary'
? $state['primary_insurer']
: end($state['dependencies']);
// Show current member info
$memberInfo = "👤 **" . ucfirst($memberData['type']) . " Member Details:**\n";
$memberInfo .= "🔹 Name: " . $memberData['name'] . "\n";
$memberInfo .= "🔹 Date of Birth: " . $memberData['dob'] . "\n";
$memberInfo .= "🔹 Sum Insured: " . $memberData['si'] . "\n";
$memberInfo .= "🔹 Pre Existing Disease: " . $memberData['pre_existing_disease'] . "\n";
$this->say($memberInfo);
$path = $this->bot->userStorage()->get('path');
if (in_array('floater', $path)) {
$this->askAboutDependencies();
} else {
$this->showSummary();
}
}
private function askAboutDependencies()
{
$question = Question::create("Do you want to add a dependency? ")
->addButtons([
Button::create("✅ Yes")->value("yes"),
Button::create("❌ No")->value("no"),
Button::create("◀️ Go Back")->value("go_back"),
]);
$this->ask($question, function($answer) {
$state = $this->bot->userStorage()->get('claim_form_state');
switch ($answer->getValue()) {
case "yes":
$state['dependency_count']++;
$state['current_member'] = 'dependency';
$this->bot->userStorage()->save(['claim_form_state' => $state]);
// Reset currentMemberData for new dependency
$this->currentMemberData = [
'name' => null,
'dob' => null,
'si' => null,
'pre_existing_disease' => null
];
$this->run();
break;
case "no":
$this->showSummary();
break;
case "go_back":
$this->bot->startConversation(new hospitalPolicyConversation());
break;
default:
$this->say("Invalid selection. Please choose an option.");
$this->askAboutDependencies();
break;
}
});
}
private function showSummary()
{
$state = $this->bot->userStorage()->get('claim_form_state');
$finalSummary = '<div style="font-family: Arial, sans-serif; font-size: 15px; margin: 0; padding: 2px;">';
// Primary Insurer
if ($state['primary_insurer']) {
$finalSummary .= '<div style="margin: 2px 0; padding-left: 5px;">Primary:<br>' .
$this->formatSingleLineMemberInfo($state['primary_insurer']) .
'</div>';
}
// Dependencies
if (!empty($state['dependencies'])) {
foreach ($state['dependencies'] as $index => $dependency) {
$finalSummary .= '<div style="margin: 2px 0; padding-left: 5px;">Dep ' . ($index + 1) . ':<br>' .
$this->formatSingleLineMemberInfo($dependency) .
'</div>';
}
}
$finalSummary .= '</div>';
$this->say($finalSummary);
$question = Question::create("Confirm all details:")
->addButtons([
Button::create("✅ Confirm")->value("confirm"),
Button::create("🔁 Start Over")->value("restart"),
Button::create("❌ Cancel")->value('cancel')
]);
$this->ask($question, function ($answer) use ($finalSummary) {
switch ($answer->getValue()) {
case "confirm":
$sent_status = ChatbotHelper::sendQuotationRequestMail($finalSummary);
if ($sent_status) {
$this->say('Our Executive will contact you with the quotation soon.');
$this->bot->startConversation(new doYouWantToContinueConversation());
} else {
$this->say("There was a problem while requesting for a quotation. Please try again later.");
}
break;
case "restart":
$this->bot->userStorage()->save([
'claim_form_state' => [
'dependency_count' => 0,
'current_member' => 'primary',
'dependencies' => [],
'primary_insurer' => null
]
]);
$this->run();
break;
case "cancel":
$this->bot->startConversation(new MainMenuConversation());
break;
default:
$this->say("Invalid selection. Please choose an option.");
$this->showSummary();
break;
}
});
}
private function formatSingleLineMemberInfo($member)
{
return '🔹 Name: ' . htmlspecialchars($member['name']) . '<br>' .
'🔹 DOB: ' . htmlspecialchars($member['dob']) . '<br>' .
'🔹 SI: ' . htmlspecialchars($member['si']) . '<br>' .
'🔹 PED: ' . htmlspecialchars($member['pre_existing_disease']);
}
// private function formatMemberInfo($member)
// {
// // Format each piece of information with proper HTML
// return '<div style="line-height: 1.6;">
// <div style="margin-bottom: 5px;">
// <span style="color: #2c3e50;">🔹 Name:</span>
// <span style="color: #34495e; font-weight: 500;">' . htmlspecialchars($member['name']) . '</span>
// </div>
// <div style="margin-bottom: 5px;">
// <span style="color: #2c3e50;">🔹 DOB:</span>
// <span style="color: #34495e; font-weight: 500;">' . htmlspecialchars($member['dob']) . '</span>
// </div>
// <div style="margin-bottom: 5px;">
// <span style="color: #2c3e50;">🔹 Sum Insured:</span>
// <span style="color: #34495e; font-weight: 500;">' . htmlspecialchars($member['si']) . '</span>
// </div>
// <div style="margin-bottom: 5px;">
// <span style="color: #2c3e50;">🔹 Pre-Existing Conditions:</span>
// <span style="color: #34495e; font-weight: 500;">' . htmlspecialchars($member['pre_existing_disease']) . '</span>
// </div>
// </div>';
// }
}