<?php 

use Phalcon\Db\Column;
use Phalcon\Db\Index;
use Phalcon\Db\Reference;
use Phalcon\Mvc\Model\Migration;

/**
 * Class FcmDeviceTokenMigration_114
 */
class FcmDeviceTokenMigration_114 extends Migration
{
    /**
     * Define the table structure
     *
     * @return void
     */
    public function morph()
    {
        $this->morphTable('fcm_device_token', [
                'columns' => [
                    new Column(
                        'id',
                        [
                            'type' => Column::TYPE_INTEGER,
                            'notNull' => true,
                            'autoIncrement' => true,
                            'first' => true
                        ]
                    ),
                    new Column(
                        'user_id',
                        [
                            'type' => Column::TYPE_INTEGER,
                            'notNull' => true,
                            'after' => 'id'
                        ]
                    ),
                    new Column(
                        'device_token',
                        [
                            'type' => Column::TYPE_TEXT,
                            'notNull' => true,
                            'size' => 1,
                            'after' => 'user_id'
                        ]
                    ),
                    new Column(
                        'created_at',
                        [
                            'type' => Column::TYPE_TIMESTAMP,
                            'default' => "now()",
                            'notNull' => true,
                            'size' => 1,
                            'after' => 'device_token'
                        ]
                    ),
                    new Column(
                        'updated_at',
                        [
                            'type' => Column::TYPE_TIMESTAMP,
                            'default' => "now()",
                            'notNull' => true,
                            'size' => 1,
                            'after' => 'created_at'
                        ]
                    ),
                    new Column(
                        'um_tenant_id',
                        [
                            'type' => Column::TYPE_INTEGER,
                            'default' => "-1234",
                            'notNull' => true,
                            'after' => 'updated_at'
                        ]
                    )
                ],
                'indexes' => [
                    new Index('fcm_device_token_pkey', ['id'], 'PRIMARY KEY')
                ]
            ]
        );
    }

    /**
     * Run the migrations
     *
     * @return void
     */
    public function up()
    {
		self::$connection->execute('ALTER TABLE jaga.fcm_device_token 
			ADD FOREIGN KEY (um_tenant_id, user_id)
			REFERENCES public.um_user (um_tenant_id, um_id) MATCH SIMPLE
			ON UPDATE NO ACTION
			ON DELETE NO ACTION');
    }

    /**
     * Reverse the migrations
     *
     * @return void
     */
    public function down()
    {
		self::$connection->execute('DROP TABLE fcm_device_token');
    }

}
