<?php
use Medoo\Medoo;
function writeToDB($address,$db,$info,$active){
	$database = new Medoo($db);
	if($active){
	$tableName = 'active_address';
	}else{
		$tableName = 'address';
	}
	$exists = $database->query("SHOW TABLES LIKE '{$tableName}'")->fetch();
	if (!$exists) {
		$database->create($tableName, [
			'id' => [
				'BIGINT',
				'UNSIGNED',
				'NOT NULL',
				'AUTO_INCREMENT',
				'PRIMARY KEY'
			],
			'address' => [
				'CHAR(42)',
				'NOT NULL',
				'UNIQUE'
			],
			'privatekey' => [
				'BINARY(32)',
				'NOT NULL'
			],
			'repeatnum' => [
				'INT',
				'NOT NULL'
			],
			'charnum' => [
				'CHAR(1)',
				'DEFAULT NULL'
			],
			'unixtime' => [
				'INT',
				'DEFAULT NULL'
			]
		], [
			'ENGINE' => 'InnoDB',
			'CHARSET' => 'utf8mb4',
			'COLLATE' => 'utf8mb4_0900_ai_ci'
		]);
		echo "✅[DB] Table `{$tableName}` Created.\n";
	}
	$info=json_decode($info, true);
	$exists = $database->has($tableName, [
    'address' => $address
	]);
	$data = [
		'address'   => $address,
		'privatekey'       => hex2bin($info['private_key']),
		'repeatnum'    => $info['count'],
		'charnum'      => $info['char'],
		'unixtime' => $info['timestamp']
	];
	if ($exists) {
		$database->update($tableName, $data, ['address' => $address]);
	} else {
		$database->insert($tableName, $data);
	}
	$id = $database->id();
	if ($id) {
	} else {
		echo "\033[31m❌[DB] DB Insert Failed\033[0m\n";
	}
}