<?php
session_start();

require("../includes/app_credentials.php");
require("includes/email-credentials.php");

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

require("../includes/db_connect.php");

function isDateThreeDaysApart($date) {
    $currentDate = new DateTime($date);  // User date and time activated
    $threeDaysLater = new DateTime();
    $threeDaysLater->modify('+3 days');

    // Compare the dates
    return $currentDate->diff($threeDaysLater)->days === 3;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['accountId']) && isset($_POST['email']) && isset($_POST['name'])) {

	$accountId = $_POST['accountId'];
	$email = $_POST['email'];
	$token = $_POST['token'];
	$name = ucfirst($_POST['name']);

	$usr_sql = "SELECT * FROM users WHERE accountId =? LIMIT 1";
	$usr_sth = $conn->prepare($usr_sql, [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
	$usr_sth->execute([$accountId]);
	$users = $usr_sth->fetchAll();

	if (count($users) == 0) {
		die("Account not found!");
	}

	$usr_sql = "SELECT * FROM users WHERE accountId =? AND email_confirmation=? LIMIT 1";
	$usr_sth = $conn->prepare($usr_sql, [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
	$usr_sth->execute([$accountId, 1]);
	$users = $usr_sth->fetchAll();

	if (count($users) > 0) {
    if (isDateThreeDaysApart($users['email_date'])) {
	$mail = new PHPMailer();

	try {

		//Server settings                          
		$mail->isSMTP();
		$mail->Host = $smtp_host;
		$mail->SMTPAuth = true;
		$mail->Username = $smtp_username;
		$mail->Password = $smtp_password;
		$mail->SMTPSecure = $smtp_encryption;
		$mail->Port = $smtp_port;

		//Recipients
		$mail->setFrom($smtp_email, $web_app_name);
		$mail->addAddress($email, $name);

		//Content
		$mail->isHTML(true);
		$mail->Subject = $name . "Getting Started - Trading On Rocketta";

		$mail->Body = "<div style=\"color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0
		auto!important;min-width:100%;padding:0;text-align:center!important;width:100%!important\">
	
		<table class=\"m_1863363065879357917body\"
			style=\"background:#ebeff6;border-collapse:collapse;border-spacing:0;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;height:100%;line-height:19px;margin:0;padding:0;text-align:left;vertical-align:top;width:100%\">
	
			<tbody>
				<tr
					style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">
	
					<td align=\"center\" valign=\"top\" class=\"m_1863363065879357917center\" style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0
						auto;padding:0;text-align:center;vertical-align:top;word-break:normal\">
	
						<center
							style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;min-width:580px;width:100%\">
	
	
							<table class=\"m_1863363065879357917container\" style=\"border-collapse:collapse;border-spacing:0;font-family:'Roboto','Helvetica','Arial',sans-serif!important;margin:0
								auto;padding:0;text-align:inherit;vertical-align:top;width:580px\">
								<tbody>
	
									<tr
										style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">
	
										<td class=\"m_1863363065879357917pt30\"
											style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;padding:0;padding-top:30px;text-align:left;vertical-align:top;word-break:normal\">
	
											<table class=\"m_1863363065879357917row m_1863363065879357917brad-top\"
												style=\"background:#fff;border-collapse:collapse;border-spacing:0;border-top-left-radius:3px!important;border-top-right-radius:3px!important;display:block;font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top;width:100%\">
	
												<tbody>
	
													<tr
														style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">
	
														<td class=\"m_1863363065879357917wrapper
															m_1863363065879357917offset-by-one m_1863363065879357917last
															m_1863363065879357917pt30\"
															style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;padding:10px
															20px 0
															0;padding-left:50px;padding-right:0;padding-top:30px;text-align:left;vertical-align:top;word-break:normal\">
	
															<table class=\"m_1863363065879357917ten
																m_1863363065879357917columns\"
																style=\"border-collapse:collapse;border-spacing:0;font-family:'Roboto','Helvetica','Arial',sans-serif!important;margin:0;padding:0;text-align:left;vertical-align:top;width:480px\">
	
																<tbody>
	
																	<tr
																		style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">
	
																		<td class=\"m_1863363065879357917six
																			m_1863363065879357917sub-columns
																			m_1863363065879357917pb0\"
																			style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;min-width:0;padding:0
																			0
																			10px;padding-bottom:0;padding-right:10px;text-align:left;vertical-align:top;width:50%;word-break:normal\">
	
																			<a href=\"$web_base_url\"
																				style=\"color:#2ba6cb;font-family:'Roboto','Helvetica','Arial',sans-serif!important;text-decoration:none\"
																				target=\"_blank\">
																				<img src=\"$web_base_url/img/logo.png\" class=\"m_1863363065879357917logo
																					CToWUd\" style=\"border:none;clear:both;display:block;float:none;font-family:'Roboto','Helvetica','Arial',sans-serif!important;outline:none;padding-bottom:0;padding-left:0;padding-top:0;text-decoration:none;width:
																					350px; height: auto;\">
																			</a>
																		</td>
																	</tr>
																</tbody>
															</table>
														</td>
													</tr>
												</tbody>
											</table>
	
	
											<table class=\"m_1863363065879357917row\"
												style=\"background:#fff;border-collapse:collapse;border-spacing:0;display:block;font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top;width:100%\">
												<tbody>
													<tr
														style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">
														<td class=\"m_1863363065879357917wrapper m_1863363065879357917last
															m_1863363065879357917offset-by-one m_1863363065879357917pt80\"
															style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;padding:10px
															20px 0
															0;padding-left:50px;padding-right:0;padding-top:80px;text-align:left;vertical-align:top;word-break:normal\">
															<table class=\"m_1863363065879357917ten
																m_1863363065879357917columns\"
																style=\"border-collapse:collapse;border-spacing:0;font-family:'Roboto','Helvetica','Arial',sans-serif!important;margin:0;padding:0;text-align:left;vertical-align:top;width:480px\">
																<tbody>
																	<tr
																		style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">
																		<td class=\"m_1863363065879357917text-pad-small
																			m_1863363065879357917pb0\"
																			style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;padding:0
																			0
																			10px;padding-bottom:0;text-align:left;vertical-align:top;word-break:normal\">
																			<p class=\"m_1863363065879357917title
																				m_1863363065879357917pb20\"
																				style=\"color:#222327;font-family:Helvetica!important;font-size:38px;font-weight:700;line-height:46px;margin:0;padding:0;padding-bottom:20px;text-align:left\">
						                  Trade on Rocketta</p>
																			<p class=\"m_1863363065879357917text
																				m_1863363065879357917pb10\"
																				style=\"color:#222327;font-family:Helvetica!important;font-size:18px;font-weight:400;line-height:28px;margin:0;padding:0;padding-bottom:10px;text-align:left\">
																				<strong
																					style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important\">Hello&nbsp;$name,</strong>
																			</p>
																			<p class=\"m_1863363065879357917text\"
																				style=\"color:#222327;font-family:Helvetica!important;font-size:18px;font-weight:400;line-height:28px;margin:0;padding:0;text-align:left\">
																			We welcome you to ROCKETTA <br> World class automated crypto trading bot.</p>
																			<p class=\"m_1863363065879357917text\"
																				style=\"color:#222327;font-family:Helvetica!important;font-size:18px;font-weight:400;line-height:28px;margin:0;padding:0;text-align:left\">
																				Earn from the comfort of your home. <br> Powerful AI powered crypto trading bot to help you save time, trade 24/7, and automate your trading.</p>
																		</td>
																	</tr>
																</tbody>
															</table>
														</td>
													</tr>
												</tbody>
											</table>
	
	
	
											<table class=\"m_1863363065879357917row\"
												style=\"background:#fff;border-collapse:collapse;border-spacing:0;display:block;font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top;width:100%\">
												<tbody>
													<tr
														style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">
														<td class=\"m_1863363065879357917wrapper m_1863363065879357917last
															m_1863363065879357917offset-by-one m_1863363065879357917pt30\"
															style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;padding:10px
															20px 0
															0;padding-left:50px;padding-right:0;padding-top:30px;text-align:left;vertical-align:top;word-break:normal\">
															<table class=\"m_1863363065879357917ten
																m_1863363065879357917columns\"
																style=\"border-collapse:collapse;border-spacing:0;font-family:'Roboto','Helvetica','Arial',sans-serif!important;margin:0;padding:0;text-align:left;vertical-align:top;width:480px\">
																<tbody>
																	<tr
																		style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">
																		<td class=\"m_1863363065879357917text-pad-small
																			m_1863363065879357917vam
																			m_1863363065879357917pb20\"
																			style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;padding:0
																			0
																			10px;padding-bottom:20px;text-align:left;vertical-align:middle!important;word-break:normal\">
																			<div class=\"m_1863363065879357917left-side\"
																				style=\"box-sizing:border-box;display:inline-block;font-family:'Roboto','Helvetica','Arial',sans-serif!important;max-width:280px;vertical-align:middle;width:60%\">
																				<div class=\"m_1863363065879357917button-wrapper\"
																					style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding-bottom:0;padding-top:0\">
																					<table class=\"m_1863363065879357917medium-button
																						m_1863363065879357917radius\"
																						style=\"border-collapse:collapse;border-spacing:0;font-family:'Roboto','Helvetica','Arial',sans-serif!important;overflow:hidden;padding:0;text-align:left;vertical-align:top;width:100%\">
																						<tbody>
																							<tr
																								style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">
																								<td class=\"m_1863363065879357917butt-td\"
																									style=\"background:#0d0c43;border:none!important;border-collapse:collapse!important;border-radius:3px!important;color:#222;display:block;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;padding:0!important;text-align:center;vertical-align:top;width:auto!important;word-break:normal\">
																									<p>For guidance on how to start using Rocketta, to get a bot and trade, kindly with the video attached below.</p> <br
																									 <iframe style=\"width: 100%;\" height=\"380\" src=\"https://www.youtube.com/embed/4jWFhu-KBsE\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen></iframe>
																								</td>
																							</tr>
																						</tbody>
																					</table>
																				</div>
																			</div>
																		</td>
																	</tr>
																</tbody>
															</table>
														</td>
													</tr>
												</tbody>
											</table>
	
											<table class=\"m_1863363065879357917row m_1863363065879357917brad-bot\"
												style=\"background:#fff;border-bottom-left-radius:3px!important;border-bottom-right-radius:3px!important;border-collapse:collapse;border-spacing:0;display:block;font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top;width:100%\">
												<tbody>
													<tr
														style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">
	
														<td class=\"m_1863363065879357917wrapper m_1863363065879357917last
															m_1863363065879357917offset-by-one m_1863363065879357917pt40\"
															style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;padding:10px
															20px 0
															0;padding-left:50px;padding-right:0;padding-top:40px;text-align:left;vertical-align:top;word-break:normal\">
															<table class=\"m_1863363065879357917ten
																m_1863363065879357917columns\"
																style=\"border-collapse:collapse;border-spacing:0;font-family:'Roboto','Helvetica','Arial',sans-serif!important;margin:0;padding:0;text-align:left;vertical-align:top;width:480px\">
																<tbody>
	
																	<tr
																		style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">
																		<td class=\"m_1863363065879357917text-pad-small
																			m_1863363065879357917pb40\"
																			style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;padding:0
																			0
																			10px;padding-bottom:40px;text-align:left;vertical-align:top;word-break:normal\">
																			<p class=\"m_1863363065879357917small-text
																				m_1863363065879357917pt5
																				m_1863363065879357917pb5\"
																				style=\"color:#474a50;font-family:Helvetica!important;font-size:14px;font-weight:400;line-height:22px;margin:0;padding:0;padding-bottom:5px;padding-top:5px;text-align:left\">
																				And keep updated on our YouTube page and other social media platform
																				</p>
																		</td>
																	</tr>
																</tbody>
															</table>
														</td>
													</tr>
												</tbody>
											</table>
	
	
											<table class=\"m_1863363065879357917row m_1863363065879357917footer\"
												style=\"background:#ebeff6;border-collapse:collapse;border-spacing:0;display:block;font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top;width:100%\">
												<tbody>
													<tr
														style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">
														<td class=\"m_1863363065879357917wrapper m_1863363065879357917last
															m_1863363065879357917offset-by-one m_1863363065879357917pt50\"
															style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;padding:10px
															20px 0
															0;padding-left:50px;padding-right:0;padding-top:50px;text-align:left;vertical-align:top;word-break:normal\">
															<table class=\"m_1863363065879357917ten
																m_1863363065879357917columns\"
																style=\"border-collapse:collapse;border-spacing:0;font-family:'Roboto','Helvetica','Arial',sans-serif!important;margin:0;padding:0;text-align:left;vertical-align:top;width:480px\">
																<tbody>
																	<tr
																		style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">
																		<td class=\"m_1863363065879357917pb50
																			m_1863363065879357917text-pad-small\"
																			style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;padding:0
																			0
																			10px;padding-bottom:50px;text-align:left;vertical-align:top;word-break:normal\">
																		</td>
																	</tr>
																</tbody>
															</table>
														</td>
													</tr>
												</tbody>
											</table>
	
	
	
	
										</td>
									</tr>
								</tbody>
							</table>
						</center>
					</td>
				</tr>
			</tbody>
		</table>
	</div>";

		$mail->send();
		createLog("email sent", "e", "logs");

		$user_sql = "UPDATE users SET email_tutorial=? WHERE accountId=?";

		$update_user = $conn->prepare($user_sql)->execute([true, $accountId]);
	} catch (Exception $e) {
		createLog("Unable to send tutorial mail: " . $mail->ErrorInfo, "e", "logs");
	}
    }
    }
} else {
	die("Invalid request");
}

function convertDate($date)
{
	$dt = str_replace('/', '-', $date);

	return date('Y-m-d H:i:s', strtotime($dt));
}

function createLog($text, $level = 'i', $file = 'logs')
{
	/*switch (strtolower($level)) {
				case 'e':
				case 'error':
				$level='ERROR';
				break;
				case 'i':
				case 'info':
				$level='INFO';
				break;
				case 'd':
				case 'debug':
				$level='DEBUG';
				break;
				default:
				$level='INFO';
			}
			error_log(date("[d-m-Y H:i:s]")."\t[".$level."]\t[".basename(__FILE__)."]\t".$text."\n", 3, $file);*/
}