<?php

use yii\db\Migration;

class m181129_054015_add_group_training_tables extends Migration
{
    public function up()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
        }
        $this->createTable('{{%trainer}}', [
            'id' => $this->primaryKey(),
            'name' => $this->string(255),
            'phone' => $this->string(20),
            'email' => $this->string(255),
            'password' => $this->string(255),
            'active' => $this->integer(),
            'created_at' => $this->dateTime()->notNull(),
            'updated_at' => $this->dateTime()->notNull(),
        ], $tableOptions);


        $this->createTable('{{%room}}', [
            'id' => $this->primaryKey(),
            'name' => $this->string(255),
            'seat_count' => $this->integer(),
            'created_at' => $this->dateTime()->notNull(),
            'updated_at' => $this->dateTime()->notNull(),
        ], $tableOptions);

        $this->createTable('{{%event_type}}', [
            'id' => $this->primaryKey(),
            'name' => $this->string(255),
            'created_at' => $this->dateTime()->notNull(),
            'updated_at' => $this->dateTime()->notNull(),
        ], $tableOptions);

        $this->createTable('{{%event}}', [
            'id' => $this->primaryKey(),
            'start' => $this->integer(),
            'end' => $this->integer(),
            'id_room' => $this->integer(),
            'id_trainer' => $this->integer(),
            'id_event_type' => $this->integer(),
            'created_at' => $this->dateTime()->notNull(),
            'updated_at' => $this->dateTime()->notNull(),
        ], $tableOptions);

        $this->createTable('{{%event_registration}}', [
            'id' => $this->primaryKey(),
            'id_event' => $this->integer(),
            'id_customer' => $this->integer(),
            'created_at' => $this->dateTime()->notNull(),
            'updated_at' => $this->dateTime()->notNull(),
            'canceled_at' => $this->dateTime()->notNull(),
        ], $tableOptions);


//        $this->execute("DELIMITER $$
//
//CREATE TRIGGER event_seat_count_check
//     AFTER INSERT ON event_registration FOR EACH ROW
//     BEGIN
//          IF (SELECT COUNT(id) FROM event_registration
//               WHERE canceled_at is null AND id_event = NEW.id_event) > ( select
//          THEN
//               SIGNAL SQLSTATE '45000'
//                    SET MESSAGE_TEXT = 'Max seat count exceeded!';
//          END IF;
//     END;
//$$");


    }

    public function down()
    {
        echo "m181129_054015_add_group_training_tables cannot be reverted.\n";

        return false;
    }

    /*
    // Use safeUp/safeDown to run migration code within a transaction
    public function safeUp()
    {
    }

    public function safeDown()
    {
    }
    */
}
