<?php 

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

/**
 * Class NotificationsMigration_138
 */
class NotificationsMigration_138 extends Migration
{
    /**
     * Define the table structure
     *
     * @return void
     */
    public function morph()
    {
        $this->morphTable('notifications', [
                'columns' => [
                    new Column(
                        'id',
                        [
                            'type' => Column::TYPE_INTEGER,
                            'notNull' => true,
                            'autoIncrement' => true,
                            'first' => true
                        ]
                    ),
                    new Column(
                        'owner_user_id',
                        [
                            'type' => Column::TYPE_INTEGER,
                            'notNull' => true,
                            'after' => 'id'
                        ]
                    ),
                    new Column(
                        'sender_user_id',
                        [
                            'type' => Column::TYPE_INTEGER,
                            'after' => 'owner_user_id'
                        ]
                    ),
                    new Column(
                        'tenant_id',
                        [
                            'type' => Column::TYPE_INTEGER,
                            'default' => "-1234",
                            'notNull' => true,
                            'after' => 'sender_user_id'
                        ]
                    ),
                    new Column(
                        'message',
                        [
                            'type' => Column::TYPE_TEXT,
                            'notNull' => true,
                            'after' => 'tenant_id'
                        ]
                    ),
                    new Column(
                        'action',
                        [
                            'type' => Column::TYPE_INTEGER,
                            'after' => 'message'
                        ]
                    ),
                    new Column(
                        'action_id',
                        [
                            'type' => Column::TYPE_TEXT,
                            'after' => 'action'
                        ]
                    ),
                    new Column(
                        'is_read',
                        [
                            'type' => Column::TYPE_INTEGER,
                            'after' => 'action_id',
							'default' => "0",
                        ]
                    )
                ],
                'indexes' => [
                    new Index('notifications_pkey', ['id'], 'PRIMARY KEY')
                ],
            ]
        );
    }

    /**
     * Run the migrations
     *
     * @return void
     */
    public function up()
    {
		self::$connection->execute(
		'
			ALTER TABLE jaga.notifications
			ADD COLUMN created_at timestamp without time zone NOT NULL DEFAULT now();
			ALTER TABLE jaga.notifications 
			ADD FOREIGN KEY (tenant_id, owner_user_id)
			REFERENCES public.um_user (um_tenant_id, um_id) MATCH SIMPLE
			ON UPDATE NO ACTION
			ON DELETE NO ACTION;
			ALTER TABLE jaga.notifications 
			ADD FOREIGN KEY (tenant_id, sender_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()
    {

    }

}
