<?php

namespace common\modules\event\models\copy;

use common\manager\EventRegistrationManager;
use common\modules\event\manager\EventManager;
use common\modules\event\models\timetable\TimeTableMonth;
use common\modules\event\models\timetable\TimeTableMonthWeek;
use customerapi\models\available\EventInterval;
use DateTime;
use Throwable;
use Yii;
use yii\base\Model;

/**
 * @property string $sourceDateString
 * @property string $targetDateString
 * @property integer $timestampSource
 * @property integer $timestampTarget
 * @property EventInterval $sourceInterval
 * @property EventInterval $targetInterval
 * @property TimeTableMonth $sourceTimeTable
 * @property TimeTableMonth $targetTimeTable
 */
class ClearWeekForm extends Model
{

    public /** @noinspection PhpUnused */
        $targetDateString;
    public $timestampTarget;


    public $targetInterval;
    public $targetTimeTable;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['targetDateString',], 'date', 'format' => Yii::$app->formatter->dateFormat, 'timestampAttribute' => 'timestampTarget', 'timeZone' => 'UTC'],
        ];
    }

    /**
     * @param $params
     * @throws Throwable
     */
    public function clear($params){
        $this->load($params);
        $this->validate();

        $targetDate =  new DateTime('@'.$this->timestampTarget );
        $targetDate->modify('this week');

        $this->targetInterval = EventInterval::createInterval($targetDate,7,7);

        // load the time table objects for source and target interval
        $eventManager = new EventManager();
        $this->targetTimeTable = $eventManager->loadTimeTable($this->targetInterval);

        /** @var TimeTableMonthWeek $targetWeek */
        $targetWeek = $this->targetTimeTable->getWeekByIndex(0);

        $events = $targetWeek->getAllEvents();

        $eventRegisterManager = new EventRegistrationManager();
        foreach ($events as $event){
            $eventRegisterManager->deleteEvent($event);
        }
    }

}
