<?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;
use yii\helpers\Json;

class Cleanup extends BaseObject implements \common\components\Queue\JobInterface
{
    public $model;
    public $id;
    public $schedule;
    public $limit;

    public function execute($queue)
    {
        echo "Starting Clean UP" . PHP_EOL;
        //print_r(Yii::$app->Datafile);
        $sysmodel = Sysmodels::findOne(['source' => $this->model]);
        $runnow = true;

        if ($runnow == true) {
            echo 'Model Copy' . PHP_EOL;
            echo  $sysmodel->destination . PHP_EOL;
            $r1 = $sysmodel->datamap['_ID'];
            if ($sysmodel->destination == '\backend\models\Importproduct' || $sysmodel->destination == '\frontend\models\import\Importproduct') {
                $model = \backend\models\Productsbe::find();
                $mod = $model->where(['product_id' => $this->id])->one();
            } else {
                $model = $sysmodel->destination::find();
                $mod = $model->where(['indexes_erpid' => $this->id])->one();
            }
            $mod ? $mod->delete() : false;
        }
    }

    /**
     * @inheritdoc
     */
    public function getTtr()
    {
        return 6000;
    }

    /**
     * @inheritdoc
     */
    public function canRetry($attempt, $error)
    {
        return $attempt < 3;
    }
}
