<?php
	class FlarumData extends CodonData {


		public static function setup() {
			$pilots = PilotData::getAllPilots();
			foreach ($pilots as $pilot) {

				$pilotdata = $pilot->code.''.PilotData::getProperPilotID($pilot->pilotid);
				$username = $pilotdata.' | '.$pilot->firstname.' '.$pilot->lastname;
				$message = self::create_user($username, $pilot->email, 'dummypassword');
				LogData::addLog(Auth::$userinfo->pilotid, $message);
			}
			return count($pilots)." accounts setup!";
		}

		public static function create_user($username, $email, $password) {


			// THIS VA DOES NOT HAVE FLARUM! ENABLING THIS CODE WITHOUT HAVING FLARUM WILL
			// CREATE PROBLEMS WHILE CREATING PILOT ACCOUNT WHEN A USER REGISTERS.

			$sql = "SELECT * FROM `flarum_users` WHERE `email` LIKE '$email'";
			$res = DB::get_results($sql);
			$id = $res->id;
			if($res == '') {
				if($username != '' && $email != '' && $password != '') {

			$password = password_hash($password, PASSWORD_DEFAULT);
			$sql = "INSERT INTO `flarum_users` (`id`, `username`, `email`, `is_email_confirmed`,
			`password`, `bio`, `avatar_url`, `preferences`, `joined_at`, `last_seen_at`,
			`marked_all_as_read_at`, `read_notifications_at`, `discussion_count`, `comment_count`,
			`read_flags_at`, `suspended_until`)
			VALUES (NULL, '".$username."', '".$email."', '1', '".$password."', NULL, NULL, NULL, CURRENT_TIMESTAMP, NULL, NULL, NULL, '0', '0', NULL, NULL)";

			$query = DB::query($sql);
			$message = "[FORUM SYSTEM] Forum user $username {$email} created. Please ask them to reset their password to use the account.";
			return $message;
			// DiscordData::deliver($message);

			} else {
				return "Error: Unable to create flarum account for $username";
			}
			} else {
				$message = "[FLARUM WARNING] Forum user with email ID {$email} already exists and has registered again, skipping....";
				// DiscordData::deliver($message);
				$sql = "DELETE FROM `flarum_users` WHERE `id` = '$id'";
				// $res = DB::query($sql);

				return $message;
			}




		}
	}
