<?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\SMSRelentless\Migration;

use Closure;
use OCP\DB\Types;
use Doctrine\DBAL\Types\Type;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;


class Version108Date20220823132408 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) {
		/** @var ISchemaWrapper $schema */
		$schema = $schemaClosure();

		if (!$schema->hasTable('sms_relent_settings')) {
			$table = $schema->createTable('sms_relent_settings');
			$table->addColumn('id', 'bigint', [
				'autoincrement' => true,
				'notnull' => true,
                                'length' => 11,
                                'unsigned' => true,
			]);
			$table->addColumn('user_id', 'string', [
				'notnull' => true,
				'length' => 128,
			]);
			$table->addColumn('telapi_key', 'string', [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('tel_pub_key', 'string', [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('telapi_url_rec', 'string', [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('telapi_url', 'string', [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('messaging_profile_id', 'string', [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('nexapi_key', 'string', [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('nexapi_secret', 'string', [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('nexapi_url_rec', 'string', [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('nexapi_url', 'string', [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('twilapi_key', 'string', [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('twilapi_secret', 'string', [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('twilapi_url_rec', 'string', [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('twilapi_url', 'string', [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('flowapi_key', 'string', [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('flowapi_secret', 'string', [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('flowapi_url_rec', 'string', [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('flowapi_url', 'string', [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('tel_sender_name', 'string', [
				'notnull' => false,
				'length' => 48,
				'default' => '',
			]);
			$table->addColumn('nex_sender_name', 'string', [
				'notnull' => false,
				'length' => 48,
				'default' => '',
			]);
			$table->addColumn('twil_sender_name', 'string', [
				'notnull' => false,
				'length' => 48,
				'default' => '',
			]);
			$table->addColumn('flow_sender_name', 'string', [
				'notnull' => false,
				'length' => 48,
				'default' => '',
			]);
			$table->addColumn('messagesperpage', 'integer', [
				'notnull' => false,
				'length' => 11,
                                'unsigned' => true,
			]);
			$table->addColumn('get_notify', 'smallint', [
				'notnull' => false,
				'length' => 1,
			]);
			$table->addColumn('notification_email', 'string', [
				'notnull' => false,
				'length' => 512,
				'default' => '',
			]);
			$table->addColumn('getsmsinemail', 'smallint', [
				'notnull' => false,
				'length' => 1,
			]);
                        $table->setPrimaryKey(['id']);
                        $table->addUniqueIndex(['id']);

		} else {

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

			$table->addColumn('flowapi_key', 'string', [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('flowapi_secret', 'string', [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('flowapi_url_rec', 'string', [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('flowapi_url', 'string', [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('flow_sender_name', 'string', [
				'notnull' => false,
				'length' => 48,
				'default' => '',
			]);

                }

		if (!$schema->hasTable('sms_relent_received')) {
			$table = $schema->createTable('sms_relent_received');
			$table->addColumn('id', 'bigint', [
				'autoincrement' => true,
				'notnull' => true,
                                'length' => 11,
                                'unsigned' => true,
			]);
			$table->addColumn('user_id', 'string', [
				'notnull' => true,
				'length' => 64,
			]);
			$table->addColumn('message_id', 'string', [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('date', 'datetime', [
				'notnull' => true,
			]);
			$table->addColumn('from', 'string', [
				'notnull' => true,
				'length' => 128,
			]);
			$table->addColumn('to', 'string', [
				'notnull' => true,
				'length' => 128,
			]);
			$table->addColumn('message', 'text', [
				'notnull' => false,
				'default' => '',
			]);
                        $table->setPrimaryKey(['id']);
                        $table->addUniqueIndex(['id']);

		}

		if (!$schema->hasTable('sms_relent_sent')) {
			$table = $schema->createTable('sms_relent_sent');
			$table->addColumn('id', 'bigint', [
				'autoincrement' => true,
				'notnull' => true,
                                'length' => 11,
                                'unsigned' => true,
			]);
			$table->addColumn('user_id', 'string', [
				'notnull' => true,
				'length' => 64,
			]);
			$table->addColumn('message_id', 'string', [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('date', 'datetime', [
				'notnull' => true,
			]);
			$table->addColumn('from', 'string', [
				'notnull' => true,
				'length' => 128,
			]);
			$table->addColumn('to', 'string', [
				'notnull' => true,
				'length' => 128,
			]);
			$table->addColumn('network ', 'string', [
				'notnull' => false,
				'length' => 64,
				'default' => '',
			]);
			$table->addColumn('price', 'string', [
				'notnull' => false,
				'length' => 64,
				'default' => '',
			]);
			$table->addColumn('status', 'string', [
				'notnull' => false,
				'length' => 512,
				'default' => '',
			]);
			$table->addColumn('deliveryreceipt', 'string', [
				'notnull' => false,
				'length' => 64,
				'default' => '',
			]);
			$table->addColumn('message', 'text', [
				'notnull' => false,
                                'default' => '',
			]);
                        $table->setPrimaryKey(['id']);
                        $table->addUniqueIndex(['id']);

		}

		return $schema;
	}
}
