<?php
###############################################################################
# Third Party Data Updater
#
#
# @author Anil Kumar <akumar@codepunch.com>
# @link   https://codepunch.com
#
############################################################################### 

require '../lib/php/loader.php';
require __DIR__. DIRECTORY_SEPARATOR . 'thirdparty/estibot.php';
require __DIR__. DIRECTORY_SEPARATOR . 'thirdparty/dotdb.php';

###############################################################################

$tpconfig = __DIR__. DIRECTORY_SEPARATOR . "config.tools.php";
if(is_file($tpconfig))
	require $tpconfig;

###############################################################################

use CodePunch\Base\Util as UTIL;
use	CodePunch\Base\Text as TEXT;
use	CodePunch\DB\Audit as AUDIT;

###############################################################################

$tool_to_run = "dotdb";

###############################################################################

if(!UTIL::is_cli()) {
	header("Location: " . UTIL::get_root_url());
	exit;
}

###############################################################################

UTIL::parse_request_data_in_cli();
$rd = UTIL::get_unsafe_request_data_array();

###############################################################################

if(isset($rd['run'])) {
	$what = $rd['run'];
	if(strtolower($what) == "estibot") {
		$tool_to_run = "estibot";
	}
}

###############################################################################

try {
	$auth = new \CodePunch\Config\Auth(\CodePunch\DB\Database::READONLY_TABLES);
	if($auth) {
		$db = $auth->getDatabase();
		$luManager = new \CodePunch\LU\LookupManager($auth);
		if($db) {
			$updated = 0;
			$days = 120;
			$maxcount = 300;
			$minutes = $days * 60 * 24;
			$compdate = date("Y-m-d H:i:s", time() - ($minutes*60));
			if($tool_to_run == "estibot") {
				$rows = $db->getFromTable("sid,domain", $db->getDomainTableName(), "estibot_checked_at < ? OR estibot_checked_at is NULL", [$compdate], "selling_price", "desc", $maxcount);
				$apikey = $ESTIBOT_API_KEY;
				foreach($rows as $row) {
					$domain = $row['domain'];
					$sid = $row['sid'];
					$estibotdata = get_estibot_apraisal_data($domain, $apikey);
					UTIL::print($estibotdata);
					break;
				}
			}
			else {
				$rows = $db->getFromTable("sid,domain", $db->getDomainTableName(), "dotdb_checked_at < ? OR dotdb_checked_at is NULL", [$compdate], "selling_price", "desc", $maxcount);
				
				foreach($rows as $row) {
					$domain = $row['domain'];
					$sid = $row['sid'];
					$dotdbdata = get_dotdb_counts($domain, $DOTDB_API_KEY);
					if($dotdbdata !== false) {
						UTIL::print($dotdbdata);
						if($dotdbdata['domain'] == $domain) {
							unset($dotdbdata['domain']);
							unset($dotdbdata['keyword']);
							$update_status = $db->updateTable($db->getDomainTableName(), $dotdbdata, "sid=?", [$sid]);
							if($update_status !== false)
								$updated += 1;
						}
					}
					sleep(1);
				}
				UTIL::print($updated);
			}
		}
	}
}
catch(Exception $e) {
	$logger = new \CodePunch\Base\CPLogger();
	$logger->error($e->getMessage());
	if(UTIL::is_cli()) {
		UTIL::print($e->getMessage());
	}
	else {
		$layout = new \CodePunch\UI\Layout();
		$pageinfo = array(
		'heading'=>'<h3>There was a Problem!</h3>',
		'body'=>$e->getMessage() . " - " . $e->getFile() . " - " . $e->getLine()
		);
		$layout->show($pageinfo);
	}
	exit;
}

###############################################################################

