<?php 

namespace frontend\jobs;
use yii;
use yii\base\BaseObject;
use yii\httpclient\Client;
use backend\models\Galleriesbe;
use backend\models\Sysmodels;
use backend\models\Categoriesbe;
use backend\models\Productsbe;
use backend\models\{Lookuptablebe, Mailqueue};

use yii\helpers\Json;

class check_df_data extends BaseObject implements \common\components\Queue\JobInterface
{
	

	    /**
     * Runs Imports from Various Sources.
     *
     * @param [type] $queue
     * @return void
     */
    public function execute($queue)
    {
        $delay = strtotime('tomorrow 09:30') - strtotime('now');
      	yii::$app->queue
            ->jobname('Check Uploaded Data')    
            ->delay($delay)
            ->ttr(3600)
            ->push(new self());
		
		$models = Sysmodels::find()
			->where(['status' => 'Active'])
			->andwhere(['like', 'source' , 'api'])
			->all();
		$content = 'The Following API&apos;s have not recieved any data so far today. <br/>';
		$errors = 0;
		foreach($models as $key => $model){
			$dateupdated = $model->date_updated_field;
			$datecreated = $model->date_created_field;
			if (empty($dateupdated) == false){
				$mod = $model->source::find()->orderby([$dateupdated => SORT_DESC])->one();
				if(date($mod->$dateupdated) < date('Y-m-d', strtotime('today'))){
					$content .= 'API: '.$model->destination.' Last Updated: '.date('d/m/Y', strtotime($mod->$dateupdated)).'<br />';
					$errors++;
				}
			}
		}
		$errors > 0   ?  
			Mailqueue::CreatenewArray([
				'to_email' => 'a.pickstock@portalpeople.uk',
				'subject' => 'Data Check',  
				'message' => $content,
				'language' => 'en',
				'attempts' => 0,
				'status' => 'active',
			], true)
		: false;
	}
	
}