<?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 OCP\DB\ISchemaWrapper;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;


class Version135Date20240420221527 extends SimpleMigrationStep {

        /** @var IDBConnection */
        private $connection;

        /**
         * @param IDBConnection $connection
         */
        public function __construct(IDBConnection $connection) {
                $this->connection = $connection;
        }

	/**
	 * @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('sms_relent_settings')) {
			$table = $schema->createTable('sms_relent_settings');
			$table->addColumn('id', Types::BIGINT, [
				'autoincrement' => true,
				'notnull' => true,
                                'length' => 11,
                                'unsigned' => true,
			]);
			$table->addColumn('user_id', Types::STRING, [
				'notnull' => true,
				'length' => 128,
			]);
			$table->addColumn('telapi_key', Types::STRING, [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('tel_pub_key', Types::STRING, [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('telapi_url_rec', Types::STRING, [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('telapi_url', Types::STRING, [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('messaging_profile_id', Types::STRING, [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('nexapi_key', Types::STRING, [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('nexapi_secret', Types::STRING, [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('nexapi_url_rec', Types::STRING, [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('nexapi_url', Types::STRING, [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('twilapi_key', Types::STRING, [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('twilapi_secret', Types::STRING, [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('twilapi_url_rec', Types::STRING, [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('twilapi_url', Types::STRING, [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('flowapi_key', Types::STRING, [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('flowapi_secret', Types::STRING, [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('flowapi_url_rec', Types::STRING, [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('flowapi_url', Types::STRING, [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('tel_sender_name', Types::STRING, [
				'notnull' => false,
				'length' => 48,
				'default' => '',
			]);
			$table->addColumn('nex_sender_name', Types::STRING, [
				'notnull' => false,
				'length' => 48,
				'default' => '',
			]);
			$table->addColumn('twil_sender_name', Types::STRING, [
				'notnull' => false,
				'length' => 48,
				'default' => '',
			]);
			$table->addColumn('flow_sender_name', Types::STRING, [
				'notnull' => false,
				'length' => 48,
				'default' => '',
			]);
			$table->addColumn('messagesperpage', Types::INTEGER, [
				'notnull' => false,
				'length' => 10,
                                'unsigned' => true,
			]);
			$table->addColumn('get_notify', Types::SMALLINT, [
				'notnull' => false,
				'length' => 1,
			]);
			$table->addColumn('notification_email', Types::STRING, [
				'notnull' => false,
				'length' => 512,
				'default' => '',
			]);
			$table->addColumn('getsmsinemail', Types::SMALLINT, [
				'notnull' => false,
				'length' => 1,
			]);
			$table->addColumn('show_all_messages', Types::SMALLINT, [
				'notnull' => false,
				'length' => 1,
			]);
			$table->addColumn('show_display_names', Types::SMALLINT, [
				'notnull' => false,
				'length' => 1,
			]);
			$table->addColumn('add_display_names', Types::SMALLINT, [
				'notnull' => false,
				'length' => 1,
			]);
			$table->addColumn('available_numbers', Types::TEXT, [
				'notnull' => false,
                                'default' => '',
			]);
			$table->addColumn('msg_check_interval', Types::INTEGER, [
				'notnull' => false,
				'length' => 10,
                                'unsigned' => true,
			]);
			$table->addColumn('new_message_rcd', Types::SMALLINT, [
				'notnull' => false,
				'length' => 1,
			]);
			$table->addColumn('archived_conv_nmbr', Types::INTEGER, [
				'notnull' => false,
				'length' => 10,
                                'unsigned' => true,
			]);
                        $table->setPrimaryKey(['id']);
                        $table->addUniqueIndex(['id']);

		}


		if (!$schema->hasTable('sms_relent_received')) {
			$table = $schema->createTable('sms_relent_received');
			$table->addColumn('id', Types::BIGINT, [
				'autoincrement' => true,
				'notnull' => true,
                                'length' => 11,
                                'unsigned' => true,
			]);
			$table->addColumn('user_id', Types::STRING, [
				'notnull' => true,
				'length' => 64,
			]);
			$table->addColumn('message_id', Types::STRING, [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('date', Types::DATETIME, [
				'notnull' => true,
			]);
			$table->addColumn('from', Types::STRING, [
				'notnull' => true,
				'length' => 128,
			]);
			$table->addColumn('to', Types::STRING, [
				'notnull' => true,
				'length' => 128,
			]);
			$table->addColumn('message', Types::TEXT, [
				'notnull' => false,
				'default' => '',
			]);
			$table->addColumn('author_displayname', Types::STRING, [
				'notnull' => true,
				'length' => 255,
			]);
			$table->addColumn('internal_sender', Types::SMALLINT, [
				'notnull' => false,
				'length' => 1,
			]);
			$table->addColumn('conversation_id', Types::STRING, [
				'notnull' => false,
				'length' => 255,
			]);
                        $table->setPrimaryKey(['id']);
                        $table->addUniqueIndex(['id']);

		}


		if (!$schema->hasTable('sms_relent_sent')) {
			$table = $schema->createTable('sms_relent_sent');
			$table->addColumn('id', Types::BIGINT, [
				'autoincrement' => true,
				'notnull' => true,
                                'length' => 11,
                                'unsigned' => true,
			]);
			$table->addColumn('user_id', Types::STRING, [
				'notnull' => true,
				'length' => 64,
			]);
			$table->addColumn('message_id', Types::STRING, [
				'notnull' => true,
				'length' => 512,
			]);
			$table->addColumn('date', Types::DATETIME, [
				'notnull' => true,
			]);
			$table->addColumn('from', Types::STRING, [
				'notnull' => true,
				'length' => 128,
			]);
			$table->addColumn('to', Types::STRING, [
				'notnull' => true,
				'length' => 128,
			]);
			$table->addColumn('network ', Types::STRING, [
				'notnull' => false,
				'length' => 64,
				'default' => '',
			]);
			$table->addColumn('price', Types::STRING, [
				'notnull' => false,
				'length' => 64,
				'default' => '',
			]);
			$table->addColumn('status', Types::STRING, [
				'notnull' => false,
				'length' => 512,
				'default' => '',
			]);
			$table->addColumn('deliveryreceipt', Types::STRING, [
				'notnull' => false,
				'length' => 64,
				'default' => '',
			]);
			$table->addColumn('message', Types::TEXT, [
				'notnull' => false,
                                'default' => '',
			]);
			$table->addColumn('author_displayname', Types::STRING, [
				'notnull' => true,
				'length' => 255,
			]);
			$table->addColumn('conversation_id', Types::STRING, [
				'notnull' => false,
				'length' => 255,
			]);
                        $table->setPrimaryKey(['id']);
                        $table->addUniqueIndex(['id']);

		}


		if (!$schema->hasTable('sms_relent_autorply')) {
			$table = $schema->createTable('sms_relent_autorply');
			$table->addColumn('id', Types::BIGINT, [
				'autoincrement' => true,
				'notnull' => true,
                                'length' => 11,
                                'unsigned' => true,
			]);
			$table->addColumn('user_id', Types::STRING, [
				'notnull' => true,
				'length' => 128,
			]);
			$table->addColumn('saved_by_dsplname', Types::STRING, [
				'notnull' => true,
				'length' => 255,
			]);
			$table->addColumn('phone_number', Types::STRING, [
				'notnull' => true,
				'length' => 128,
			]);
			$table->addColumn('days_of_week', Types::STRING, [
				'notnull' => false,
				'length' => 64,
                                'default' => '',
			]);
			$table->addColumn('daily_start', Types::STRING, [
				'notnull' => false,
				'length' => 8,
                                'default' => '',
			]);
			$table->addColumn('daily_end', Types::STRING, [
				'notnull' => false,
				'length' => 8,
                                'default' => '',
			]);
			$table->addColumn('vacation_start', Types::DATETIME, [
				'notnull' => false,
			]);
			$table->addColumn('vacation_end', Types::DATETIME, [
				'notnull' => false,
			]);
			$table->addColumn('message_text', Types::TEXT, [
				'notnull' => false,
                                'default' => '',
			]);
                        $table->setPrimaryKey(['id']);
                        $table->addUniqueIndex(['id']);
		}


		if (!$schema->hasTable('sms_relent_restrict')) {
			$table = $schema->createTable('sms_relent_restrict');
			$table->addColumn('id', Types::BIGINT, [
				'autoincrement' => true,
				'notnull' => true,
                                'length' => 11,
                                'unsigned' => true,
			]);
			$table->addColumn('user_id', Types::STRING, [
				'notnull' => true,
				'length' => 128,
			]);
			$table->addColumn('saved_by_dsplname', Types::STRING, [
				'notnull' => true,
				'length' => 255,
			]);
			$table->addColumn('phone_number', Types::STRING, [
				'notnull' => true,
				'length' => 128,
			]);
			$table->addColumn('groups', Types::STRING, [
				'notnull' => true,
				'length' => 2048,
			]);
			$table->addColumn('users', Types::STRING, [
				'notnull' => true,
				'length' => 2048,
			]);
                        $table->setPrimaryKey(['id']);
                        $table->addUniqueIndex(['id']);
		}


		if (!$schema->hasTable('sms_relent_subac')) {
			$table = $schema->createTable('sms_relent_subac');
			$table->addColumn('id', Types::BIGINT, [
				'autoincrement' => true,
				'notnull' => true,
                                'length' => 11,
                                'unsigned' => true,
			]);
			$table->addColumn('user_id', Types::STRING, [
				'notnull' => true,
				'length' => 128,
			]);
			$table->addColumn('tnx_groups_allowed', Types::TEXT, [
				'notnull' => false,
                                'default' => '',
			]);
			$table->addColumn('tnx_users_allowed', Types::TEXT, [
				'notnull' => false,
                                'default' => '',
			]);
			$table->addColumn('plv_groups_allowed', Types::TEXT, [
				'notnull' => false,
                                'default' => '',
			]);
			$table->addColumn('plv_users_allowed', Types::TEXT, [
				'notnull' => false,
                                'default' => '',
			]);
			$table->addColumn('twl_groups_allowed', Types::TEXT, [
				'notnull' => false,
                                'default' => '',
			]);
			$table->addColumn('twl_users_allowed', Types::TEXT, [
				'notnull' => false,
                                'default' => '',
			]);
			$table->addColumn('flr_groups_allowed', Types::TEXT, [
				'notnull' => false,
                                'default' => '',
			]);
			$table->addColumn('flr_users_allowed', Types::TEXT, [
				'notnull' => false,
                                'default' => '',
			]);
			$table->addColumn('tnx_groups_del', Types::SMALLINT, [
				'notnull' => false,
				'length' => 1,
			]);
			$table->addColumn('tnx_users_del', Types::SMALLINT, [
				'notnull' => false,
				'length' => 1,
			]);
			$table->addColumn('plv_groups_del', Types::SMALLINT, [
				'notnull' => false,
				'length' => 1,
			]);
			$table->addColumn('plv_users_del', Types::SMALLINT, [
				'notnull' => false,
				'length' => 1,
			]);
			$table->addColumn('twl_groups_del', Types::SMALLINT, [
				'notnull' => false,
				'length' => 1,
			]);
			$table->addColumn('twl_users_del', Types::SMALLINT, [
				'notnull' => false,
				'length' => 1,
			]);
			$table->addColumn('flr_groups_del', Types::SMALLINT, [
				'notnull' => false,
				'length' => 1,
			]);
			$table->addColumn('flr_users_del', Types::SMALLINT, [
				'notnull' => false,
				'length' => 1,
			]);
                        $table->setPrimaryKey(['id']);
                        $table->addUniqueIndex(['id']);

		} else {

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

			$table->addColumn('tnx_groups_del', Types::SMALLINT, [
				'notnull' => false,
				'length' => 1,
			]);
			$table->addColumn('tnx_users_del', Types::SMALLINT, [
				'notnull' => false,
				'length' => 1,
			]);
			$table->addColumn('plv_groups_del', Types::SMALLINT, [
				'notnull' => false,
				'length' => 1,
			]);
			$table->addColumn('plv_users_del', Types::SMALLINT, [
				'notnull' => false,
				'length' => 1,
			]);
			$table->addColumn('twl_groups_del', Types::SMALLINT, [
				'notnull' => false,
				'length' => 1,
			]);
			$table->addColumn('twl_users_del', Types::SMALLINT, [
				'notnull' => false,
				'length' => 1,
			]);
			$table->addColumn('flr_groups_del', Types::SMALLINT, [
				'notnull' => false,
				'length' => 1,
			]);
			$table->addColumn('flr_users_del', Types::SMALLINT, [
				'notnull' => false,
				'length' => 1,
			]);
                }


		if (!$schema->hasTable('sms_relent_conv')) {
			$table = $schema->createTable('sms_relent_conv');
			$table->addColumn('id', Types::BIGINT, [
				'autoincrement' => true,
				'notnull' => true,
                                'length' => 11,
                                'unsigned' => true,
			]);
			$table->addColumn('conversation_id', Types::STRING, [
				'notnull' => false,
				'length' => 255,
			]);
			$table->addColumn('archived', Types::SMALLINT, [
				'notnull' => false,
				'length' => 1,
			]);
			$table->addColumn('last_archived', Types::DATETIME, [
				'notnull' => false,
			]);
			$table->addColumn('last_unarchived', Types::DATETIME, [
				'notnull' => false,
			]);
			$table->addColumn('archived_by', Types::STRING, [
				'notnull' => false,
				'length' => 128,
			]);
			$table->addColumn('unarchived_by', Types::STRING, [
				'notnull' => false,
				'length' => 128,
			]);
			$table->addColumn('last_msg_date', Types::DATETIME, [
				'notnull' => true,
			]);
			$table->addColumn('last_msg_from', Types::STRING, [
				'notnull' => true,
				'length' => 128,
			]);
			$table->addColumn('last_msg_to', Types::STRING, [
				'notnull' => true,
				'length' => 128,
			]);
			$table->addColumn('last_message', Types::TEXT, [
				'notnull' => false,
				'default' => '',
			]);
			$table->addColumn('lastmsgdisplayname', Types::STRING, [
				'notnull' => true,
				'length' => 255,
			]);
			$table->addColumn('flagged', Types::SMALLINT, [
				'notnull' => false,
				'length' => 1,
			]);
			$table->addColumn('flagunflagby', Types::STRING, [
				'notnull' => false,
				'length' => 128,
			]);
			$table->addColumn('flagunflagdate', Types::DATETIME, [
				'notnull' => false,
			]);
			$table->addColumn('tag', Types::STRING, [
				'notnull' => false,
				'length' => 100,
			]);
			$table->addColumn('taguntagby', Types::STRING, [
				'notnull' => false,
				'length' => 128,
			]);
			$table->addColumn('taguntagdate', Types::DATETIME, [
				'notnull' => false,
			]);
			$table->addColumn('description', Types::TEXT, [
				'notnull' => false,
                                'default' => '',
			]);
			$table->addColumn('descriptionby', Types::STRING, [
				'notnull' => false,
				'length' => 128,
			]);
			$table->addColumn('descriptiondate', Types::DATETIME, [
				'notnull' => false,
			]);
			$table->addColumn('lastmsgprovid', Types::STRING, [
				'notnull' => false,
				'length' => 512,
                                'default' => '',
			]);
                        $table->setPrimaryKey(['id']);
                        $table->addUniqueIndex(['id']);

		} else {

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

			$table->addColumn('lastmsgprovid', Types::STRING, [
				'notnull' => false,
				'length' => 512,
                                'default' => '',
			]);
               }

		return $schema;
	}

}
