<?php
/**
 * @copyright 2021 Double Bastion LLC <www.doublebastion.com>
 *
 * @author Double Bastion LLC
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

declare(strict_types=1);

namespace OCA\SIPTripPhone\Migration;

use Closure;
use OCP\DB\Types;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;


class Version115Date20240521091835 extends SimpleMigrationStep {
	/**
	 * @param IOutput $output
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
	 * @param array $options
	 * @return null|ISchemaWrapper
	 */
	public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
		/** @var ISchemaWrapper $schema */
		$schema = $schemaClosure();

		if (!$schema->hasTable('sip_trip_phone')) {
			$table = $schema->createTable('sip_trip_phone');
			$table->addColumn('id', Types::BIGINT, [
				'autoincrement' => true,
				'notnull' => true,
                                'length' => 11,
                                'unsigned' => true,
			]);
			$table->addColumn('user_id', Types::STRING, [
				'notnull' => true,
				'length' => 64,
				'default' => '',
			]);
			$table->addColumn('pdisplayname', Types::STRING, [
				'notnull' => false,
				'length' => 128,
				'default' => '',
			]);
			$table->addColumn('sipusername', Types::STRING, [
				'notnull' => false,
				'length' => 128,
				'default' => '',
			]);
			$table->addColumn('sipuserpassword', Types::STRING, [
				'notnull' => false,
				'length' => 2048,
				'default' => '',
			]);
			$table->addColumn('stphwssurl', Types::STRING, [
				'notnull' => false,
				'length' => 128,
				'default' => '',
			]);
			$table->addColumn('siprealm', Types::STRING, [
				'notnull' => false,
				'length' => 64,
				'default' => '',
			]);
			$table->addColumn('stunserver', Types::STRING, [
				'notnull' => false,
				'length' => 256,
				'default' => '',
			]);
			$table->addColumn('tracesipmsg', Types::SMALLINT, [
				'notnull' => false,
				'length' => 1,
			]);
			$table->addColumn('voicenumbers', Types::STRING, [
				'notnull' => false,
				'length' => 2048,
				'default' => '',
			]);
			$table->addColumn('defaultvoicenumber', Types::STRING, [
				'notnull' => false,
				'length' => 48,
				'default' => '',
			]);
                        $table->setPrimaryKey(['id']);
                        $table->addUniqueIndex(['id']);

		} else {

                        $table = $schema->getTable('sip_trip_phone');

			$table->changeColumn('pdisplayname', [
				'notnull' => false,
			]);
			$table->changeColumn('sipusername', [
				'notnull' => false,
			]);
			$table->changeColumn('sipuserpassword', [
				'notnull' => false,
			]);
			$table->changeColumn('stphwssurl', [
				'notnull' => false,
			]);
			$table->changeColumn('siprealm', [
				'notnull' => false,
			]);
                }

		return $schema;
	}
}
