<?php

namespace MGModule\DNSManager2\mgLibs\custom\task;

use MGModule\DNSManager2\mgLibs\custom\manager\GlobalSettingHelper;
use MGModule\DNSManager2\models\custom\task;
use MGModule\DNSManager2\models\custom\globalsetting\GlobalSettingEnum;
use MGModule\DNSManager2\models\custom\notification;
use \MGModule\DNSManager2\mgLibs\custom\manager\LogHelper;
use \MGModule\DNSManager2\mgLibs\custom\task\TaskTypeCodesCodes;

class NotificationCleaner extends TaskAbstract
{
    protected $abort_after_repeats = array('main' => 0);
    protected $taskTypeCode = TaskTypeCodesCodes::NOTIFICATIONCLEANER;
    
    public function mainDescription()
    {
        return 'Notification Cleaner';
    }
    
    public function main($params)
    {
        $this->setStatus(task\TaskStatusEnum::IN_PROGRESS);
        LogHelper::addSuccessLog($this->mainDescription().' ', 'Cron '.$this->mainDescription().' Started');

        $older_than = GlobalSettingHelper::getSetting(GlobalSettingEnum::DELETE_NOTIFICATIONS_OLDER_THAN);
        $date = date('Y-m-d H:i:s', strtotime("-$older_than days"));
        
        $rep = new notification\Repository();
        $rep->setFilter(0, array(
            'customQuery' => '`date` < :date ',
            'params' => array('date' => $date),
        ));
        
        $notCount = $rep->count();
        $rep->delete();
        
        $notificationCountWord = $notCount == 1 ? 'Notification' : 'Notifications';
        LogHelper::addSuccessLog($this->mainDescription().' ', 'End Of Cron '.$this->mainDescription().' Run - '.$notCount.' '.$notificationCountWord.' Deleted');    
    }
}
