<?php

declare(strict_types=1);

use Phinx\Db\Adapter\MysqlAdapter;
use Phinx\Migration\AbstractMigration;

final class CreateAnnouncement extends AbstractMigration
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change(): void
    {
        $exists = $this->hasTable('announcement');
        if (!$exists) {
            $table = $this->table('announcement', ['comment' => '公告']);
            $table->addColumn('department_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'null' => false, 'default' => 0, 'comment' => '部门/渠道id'])
                ->addColumn('title', 'string', ['limit' => 255, 'null' => false, 'default' => '', 'comment' => '内容'])
                ->addColumn('content', 'string', ['limit' => 255, 'null' => false, 'default' => '', 'comment' => '姓名'])
                ->addColumn('valid_time', 'datetime', ['null' => true, 'comment' => '有效时间'])
                ->addColumn('push_time', 'datetime', ['null' => true, 'comment' => '发布时间'])
                ->addColumn('status', 'integer', ['limit' => MysqlAdapter::INT_TINY, 'null' => false, 'default' => 1, 'comment' => '狀態，0=停用, 1=啟用'])
                ->addColumn('priority', 'integer', ['limit' => MysqlAdapter::INT_TINY, 'null' => true, 'comment' => '优先级'])
                ->addColumn('admin_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'null' => false, 'default' => 0, 'comment' => '管理员id'])
                ->addColumn('admin_name', 'string', ['limit' => 20, 'null' => false, 'default' => '', 'comment' => '管理员名称'])
                ->addColumn('created_at', 'datetime', ['comment' => '创建时间'])
                ->addColumn('updated_at', 'datetime', ['comment' => '更新时间'])
                ->addColumn('deleted_at', 'datetime', ['comment' => '删除时间'])
                ->addIndex(['department_id'], ['name' => 'idx_department_id'])
                ->addIndex(['admin_id'], ['name' => 'idx_admin_id'])
                ->create();
        }
    }
}
