<?php
/**
 * Created by IntelliJ IDEA.
 * User: rocho
 * Date: 2018.08.29.
 * Time: 21:58
 */

namespace customerapi\controllers;


use common\models\EventRegistration;
use customerapi\manager\EventRegistrationManager;
use common\models\CardEventRegistrationForm;
use common\models\Customer;
use Exception;
use Yii;
use yii\web\Response;

/** @noinspection PhpUnused */

class EventRegistrationController extends CustomerApiController
{

    /** @noinspection PhpUnused */
    /**
     * @return Response
     * @throws Exception
     */
    public function actionIndex()
    {
       $registrationManager = new EventRegistrationManager();
        return $this->asJson( $registrationManager->findCustomerRegistrationsWithEvent(\Yii::$app->user->id) );
    }

    /** @noinspection PhpUnused */
    /***
     * @param $id_registration
     * @return Response
     * @throws Exception
     */
    public function actionRegistration($id_registration)
    {
        $registrationManager = new EventRegistrationManager();
        return $this->asJson(
                $registrationManager->findRegistration($id_registration,\Yii::$app->user->id)
            );
    }

    /**
     * @noinspection PhpUnused
     * @param $id_event
     */
    public function actionRegister($id_event) {
        /** @var Customer $customer */
        $customer = Yii::$app->user->getIdentity();
        $card =$customer->card;

        $form = new CardEventRegistrationForm();
        $form->event_id = $id_event;
        $form->card_number = $card->number;

        $manager = new \common\manager\EventRegistrationManager();
        $manager->registerCard($form);

        return $form->registration;

    }


    /**
     * Cancel a registration by id
     * @noinspection PhpUnused
     * @param $idRegistraton
     * @throws Throwable
     * @return Response
     */
    public function actionCancel($idRegistration) {
        $manager = new \common\manager\EventRegistrationManager();
        $registration = $manager->loadRegistration($idRegistration,\Yii::$app->user->id);
        $manager->cancelRegistrationTX($registration,\Yii::$app->user->id, EventRegistration::CANCEL_REASON_CUSTOMER);
        $registration = $manager->loadRegistration($idRegistration,\Yii::$app->user->id);
        return $this->asJson($registration);
    }

}
