<?php

namespace common\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "event_registration".
 *
 * @property integer $id
 * @property integer $id_event
 * @property integer $id_customer
 * @property integer $id_card
 * @property integer $id_ticket
 * @property string $created_at
 * @property string $updated_at
 * @property string $canceled_at
 * @property string $deleted_at
 */
class EventRegistration extends \yii\db\ActiveRecord
{
    const CANCEL_REASON_CUSTOMER = "customer";
    const CANCEL_REASON_EVENT = "event";

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'event_registration';
    }

    /**
     * @inheritdocd
     */
    public function rules()
    {
        return [
            [['id_event', 'id_customer'], 'integer'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('event-registration', 'ID'),
            'id_event' => Yii::t('event-registration', 'Id Event'),
            'id_customer' => Yii::t('event-registration', 'Id Customer'),
            'created_at' => Yii::t('event-registration', 'Created At'),
            'updated_at' => Yii::t('event-registration', 'Updated At'),
            'canceled_at' => Yii::t('event-registration', 'Canceled At'),
            'deleted_at' => Yii::t('event-registration', 'Deleted At'),
        ];
    }

    public function behaviors()
    {
        return ArrayHelper::merge([
            [
                'class' => TimestampBehavior::className(),
                'value' => function () {
                    return date('Y-m-d H:i:s');
                }
            ]
        ],
            parent::behaviors());
    }


    public function getEvent()
    {
        return $this->hasOne($this->getEventClass(), ['id' => 'id_event']);
    }

    public function getCustomer()
    {
        return $this->hasOne($this->getCustomerClass(), ['id' => 'id_customer']);
    }


    public function getEventClass()
    {
        return Event::class;
    }

    public function getCustomerClass()
    {
        return Customer::class;
    }

    /**
     * @param EventRegistration $eventRegistration
     */
    public static function isActive($eventRegistration)
    {
        if (!isset($eventRegistration)) {
            return false;
        }
        if (isset($eventRegistration->canceled_at)) {
            return false;
        }

        if (isset($eventRegistration->deleted_at)) {
            return false;
        }

        return true;
    }

    /**
     * @param EventRegistration $eventRegistration
     */
    public static function isForCustomer($eventRegistration, $idCustomer)
    {
        if (!isset($eventRegistration)) {
            return false;
        }
        if (!isset($eventRegistration->id_customer)) {
            return false;
        }

        return $eventRegistration->id_customer == $idCustomer;
    }

    /**
     * @param EventRegistration[] $eventRegistrations
     */
    public static function filterActive($eventRegistrations)
    {
        if (!isset($eventRegistrations)) {
            return [];
        }
        return array_filter($eventRegistrations, EventRegistration::class . '::isActive');
    }

    /**
     * @param EventRegistration[] $eventRegistrations
     */
    public static function filterForCustomer($eventRegistrations, $idCustomer)
    {
        $result = [];
        if (isset($eventRegistrations)) {
            foreach ($eventRegistrations as $eventRegistration) {
                if (EventRegistration::isForCustomer($eventRegistration, $idCustomer)) {
                    $result[] = $eventRegistration;
                }
            }
        }
        return $result;
    }
}
