<?php


##########################
# To update chromedriver #
##########################
# Go to Putty
# Log in as admin - administrator@durman.co.uk
# Find out new version number  -  google-chrome --version
# Version number to go into URL after next step
# Go to - cd /tmp
# wget -N https://storage.googleapis.com/chrome-for-testing-public/149.0.7827.54/linux64/chromedriver-linux64.zip
# Check here for version numbers - https://googlechromelabs.github.io/chrome-for-testing/#stable
# unzip chromedriver-linux64.zip
# cd chromedriver-linux64
# sudo mv chromedriver /usr/bin

// suppress all warnings
error_reporting(E_ERROR | E_PARSE);

require_once('/app/vendor/autoload.php');

use DiDom\Document;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\WebDriverBy;




// Define color codes
$colours = [
    'red' => "\033[31m",
    'green' => "\033[32m",
    'blue' => "\033[34m",
    'reset' => "\033[0m"
];

// get the attribute called id from the url
$params = [];
$id = null;
$idEnd = null;

if (isset($argv) && is_array($argv)) {
    foreach ($argv as $arg) {
        $parts = explode('=', $arg);
        if (count($parts) == 2) {
            $params[$parts[0]] = $parts[1];
        }
    }
}

$id = $params['id'] ?? null;

// if id contains a - then get the range of ids
if (!empty($id) && strpos($id, '-') !== false) {
    $ids = explode('-', $id);
    $id = $ids[0];
    $idEnd = $ids[1];
}

//###### Get database connection ######
try {
    $conn = mysqli_init();
    $conn = mysqli_connect('161.35.166.232', 'mortgage-quote', 'PJQiiftIZ68F1JC', 'mortgage_quote') or die('Could not contact database');
    mysqli_set_charset($conn, 'utf8mb4');
  

    if (mysqli_connect_errno()) {
        $data['dberror'] = 'Failed to connect to MySQL: ' . mysqli_connect_error();
    }
} catch (Exception $e) {
    $data['dberror'] = "Connection failed: " . $e->getMessage();
}

//###### If no id is passed, get all records from the table ######
if (empty($id)) {
    //loop through the table called rate-harvest
    $sql = "select * from `rate-harvest`";
} else if (!empty($idEnd)) {
    // get the range of records
    $sql = "select * from `rate-harvest` where id >= $id and id <= $idEnd";
} else {
    // get the record with the id
    $sql = "select * from `rate-harvest` where id = $id";
}

require_once('harvest-functions.php');


//###### Loop through the records ######
$query = mysqli_query($conn, $sql);
$recordCount = mysqli_num_rows($query);
$recordNumber = 1;
while ($result = mysqli_fetch_array($query)) {
    $active = $result['active'];
    $name = $result['name'];
    $link = $result['url'];
    $attribute = $result['attribute_value'];
    $fileType = $result['file_type'];
    $pdfDirectLink = $result['pdf_direct_link'];
    $loadTime = $result['load_time'];
    $useServer = $result['server'];
    $redirect = $result['redirect'];
    $useWget = $result['use_wget'];
    $lender = $result['lender'];
    $id = $result['id'];
    $lenderId = $result['lender_id'];
    $method = $result['method'];
    $newHtml = $result['new_html'];
  

    
    if ($active != -1) {
        echo ($colours['blue'] . $name . ' is not active. Skipping...' . $colours['reset'] . PHP_EOL);
        $recordNumber++;
        continue;
    }


    // if lender is investec and it's a friday then send Notion message
    if ((($lender == 'Investec') || ($lender == 'Quantum Mortgages')) && date('l') == 'Friday') {
        addNotionTask($name, 'Harvest Check needed - Id = ' . $id, $description, $lenderId, $method, 'Not started');
        $recordNumber++;
        continue;
    } else if ((($lender == 'Investec') || ($lender == 'Quantum Mortgages')) && date('l') != 'Friday') {
        echo ('Lender is ' . $lender . '. Come back Friday ...' . PHP_EOL);
        $recordNumber++;
        continue;
    }


    if ($loadTime != 0) {
        $webDriver = 'true';
    } else {
        $webDriver = 'false';
    }


    echo ('' . PHP_EOL); // Use this to create a paragraph between lenders
    echo $colours['green'] . ($recordNumber . ' / ' . $recordCount . ' - ' . $name . '[' . $id . ']' . $colours['reset'] . PHP_EOL); // Use this to create a paragraph between lenders

    $html = getDataFromPage($link, $attribute, $name, $loadTime, $webDriver, $pdfDirectLink);
    if ($html === false) {
        echo ($colours['red'] . 'Error in Harvest - Please check' . $colours['reset'] . PHP_EOL);
        addNotionTask($name, 'Harvest Check needed - Id = ' . $id, $description, $lenderId, $method, 'Broken');
        $recordNumber++;
        continue;
    }

    // echo $html; exit;


    if ($pdfDirectLink == 'true') {
        $html = cleanUpHTML($html);
    } else if ($fileType == 'pdf') {
        $html = getSnippetFromHTML($attribute, $name, $html);
        if ($html != '') {
            $url = getLinkFromHtml($html, $link);
            if ($url === false) {
                echo ($colours['red'] . 'No link for PDF found' . $colours['reset'] . PHP_EOL);
                addNotionTask($name, 'No Link for PDF found - Id = ' . $id, $description, $lenderId, $method, 'Broken');
                $recordNumber++;
                continue;
            }
            $html = getDataFromPagePdfLink($url, $name);
            if ($html === false) {
                echo ($colours['red'] . 'Error in Harvest - Please check' . $colours['reset'] . PHP_EOL);
                addNotionTask($name, 'Harvest Check needed - Id = ' . $id, $description, $lenderId, $method, 'Broken');
                $recordNumber++;
                continue;
            }
        } else {
            echo ($colours['red'] . 'Error in Harvest - Please check' . $colours['reset'] . PHP_EOL);
            addNotionTask($name, 'Harvest Check needed - Id = ' . $id, $description, $lenderId, $method, 'Broken');
            $recordNumber++;
            continue;
        }
    } else {
        $html = getSnippetFromHTML($attribute, $name, $html);
        if ($html != '') {
            $html = cleanUpHTML($html);
        } else {
            echo ($colours['red'] . 'Error in Harvest - Please check' . $colours['reset'] . PHP_EOL);
            addNotionTask($name, 'Harvest Check needed - Id = ' . $id, $description, $lenderId, $method, 'Broken');
            $recordNumber++;
            continue;
        }
    }


    //echo $html . ' - ' . $newHtml . PHP_EOL;
    $snipptiCheck = checkIfSnippetHasChanged($html, $newHtml);
    // echo ($html . PHP_EOL . PHP_EOL . PHP_EOL . PHP_EOL . PHP_EOL . $newHtml); //For testing - to see the snippet that is being compared
    if ($snipptiCheck) {
        if ($fileType == 'pdf') {
            moveToOld($name, 'pdf', '', '');
        } else {
            moveToOld($name, 'html', $html,$newHtml);
        }
        echo ($colours['red'] . 'Rate may have changed!' . $colours['reset'] . PHP_EOL);
        
        // Move new_html to old_html, update new_html in the database and set the last_updated to now
        $sql = "update `rate-harvest` set `old_html` = `new_html`, `new_html` = '" . mysqli_real_escape_string($conn, $html) . "', `last_updated` = NOW() where id = " . $id;
        mysqli_query($conn, $sql);

        addNotionTask($name, $link, $description, $lenderId, $method, 'Not started');
    } else {
        echo ($colours['green'] . 'Rate has not changed.' . $colours['reset'] . PHP_EOL);
    }
    

    $recordNumber++;
    
   
}
