<?php

use ExpressionEngine\Service\Migration\Migration;

class CreateJcogsImgProErrorNotificationsTable extends Migration
{
    /**
     * Execute the migration
     */
    public function up()
    {
        $fields = [
            'id' => [
                'type' => 'int',
                'constraint' => 11,
                'unsigned' => true,
                'auto_increment' => true
            ],
            'site_id' => [
                'type' => 'int',
                'constraint' => 4,
                'unsigned' => true,
                'default' => 1
            ],
            'operation_type' => [
                'type' => 'varchar',
                'constraint' => 50,
                'default' => 'unknown'
            ],
            'error_message' => [
                'type' => 'text',
                'null' => false
            ],
            'error_code' => [
                'type' => 'int',
                'constraint' => 11,
                'default' => 0
            ],
            'error_file' => [
                'type' => 'varchar',
                'constraint' => 500,
                'null' => true
            ],
            'error_line' => [
                'type' => 'int',
                'constraint' => 11,
                'null' => true
            ],
            'stack_trace' => [
                'type' => 'longtext',
                'null' => true
            ],
            'context_data' => [
                'type' => 'longtext',
                'null' => true
            ],
            'template_name' => [
                'type' => 'varchar',
                'constraint' => 255,
                'null' => true
            ],
            'tag_parameters' => [
                'type' => 'longtext',
                'null' => true
            ],
            'user_agent' => [
                'type' => 'text',
                'null' => true
            ],
            'ip_address' => [
                'type' => 'varchar',
                'constraint' => 45,
                'null' => true
            ],
            'created_at' => [
                'type' => 'int',
                'constraint' => 10,
                'unsigned' => true,
                'null' => false
            ],
            'is_read' => [
                'type' => 'tinyint',
                'constraint' => 1,
                'default' => 0,
                'unsigned' => true
            ]
        ];

        ee()->dbforge->add_field($fields);
        ee()->dbforge->add_key('id', true);
        ee()->dbforge->add_key(['site_id', 'is_read']);
        ee()->dbforge->add_key('created_at');
        ee()->dbforge->create_table('jcogs_img_pro_error_notifications', true);
    }

    /**
     * Rollback the migration
     */
    public function down()
    {
        ee()->dbforge->drop_table('jcogs_img_pro_error_notifications');
    }
}