<?php

class MVC_Library_Process 
{
	public $_sanitize = false;
	public $_guzzle = false;
	public $_lex = false;

	public function webhooks($uid, $type, $payload, $webhooks)
	{
		$webhookArray = [];

		if(!empty($webhooks)):
			foreach($webhooks as $webhook):
				$form = [
					"secret" => $webhook["secret"],
					"type" => $type,
					"data" => $payload
				];

				if($this->_sanitize->isUrl($webhook["url"])):
					try {
						$this->_guzzle->post($webhook["url"], [
				            "form_params" => $form,
				            "allow_redirects" => true,
				            "http_errors" => false
				        ]);

				        $webhookArray[] = $webhook["id"];
					} catch(Exception $e){
						// Ignore
					}
				endif;
			endforeach;
		endif;

		return $webhookArray;
	}

	public function actionHooks($uid, $source, $event, $phone, $message, $hooks)
	{
		$actionArray = [];

		if(!empty($hooks)):
			foreach($hooks as $hook):
				if($source == $hook["source"] && $event == $hook["event"]):
					if($this->_sanitize->isUrl($hook["link"])):
						try {
							$this->_guzzle->get($this->_lex->parse($hook["link"], [
		        				"phone" => urlencode($phone),
		        				"message" => urlencode($message),
		        				"date" => [
		        					"now" => urlencode(date("F j, Y")),
		        					"time" => urlencode(date("h:i A"))
		        				]
		        			]), [
					            "allow_redirects" => true,
					            "http_errors" => false
					        ]);

					        $actionArray[] = $hook["id"];
						} catch(Exception $e){
							// Ignore
						}
					endif;
				endif;
			endforeach;
		endif;

		return $actionArray;
	}

	public function actionAutoreplies($uid, $source, $phone, $message, $autoreplies, $subscription = [], $extra = [])
	{
		$actionArray = [];

		if(!empty($autoreplies)):
			foreach($autoreplies as $autoreply):
				if($source == $autoreply["source"]):
					$detected = false;
					$keywords = array_map("trim", explode(",", trim($autoreply["keywords"])));

					if(!empty($keywords)):
						if($this->checkKeywords($message, $keywords)):
							$detected = true;
						endif;

						if($detected):
							if(isset($extra["account"])):
								$message = [
									"text" => $this->_lex->parse(footermark($subscription["footermark"], $autoreply["message"], system_message_mark), [
										"phone" => $phone,
										"message" => $message,
										"date" => [
											"now" => date("F j, Y"),
											"time" => date("h:i A") 
										]
									])
								];

								$actionArray[] = [
									"account" => $extra["account"],
									"message" => json_encode($message)
								];
							else:
								$actionArray[] = [
									"message" => $this->_lex->parse(footermark($subscription["footermark"], $autoreply["message"], system_message_mark), [
										"phone" => $phone,
										"message" => $message,
										"date" => [
											"now" => date("F j, Y"),
											"time" => date("h:i A") 
										]
									])
								];
							endif;
						endif;
					endif;
				endif;
			endforeach;
		endif;

		return $actionArray;
	}

	private function checkKeywords($string, $keywords, $caseSensitive = false) : bool
	{
		$flags = $caseSensitive ? '' : 'i'; // Add the 'i' flag for case-insensitive matching

		foreach ($keywords as $keyword) {
			// Escape any special characters in the keyword, then create a regex pattern
			$escapedKeyword = preg_quote($keyword, '/');
			$pattern = "/\b{$escapedKeyword}\b/{$flags}";

			if (preg_match($pattern, $string)) {
				return true;
			}
		}

		return false;
	}
}