module.exports = {
  /**
   * @param {QueryInterface} queryInterface
   * @param {Sequelize} Sequelize
   * @returns {Promise<void>}
   */
  async up(queryInterface, Sequelize) {
    /**
     * @type {Transaction}
     */
    const transaction = await queryInterface.sequelize.transaction();
    try {
      await queryInterface.addColumn(
        'organizations',
        'name',
        {
          type: Sequelize.DataTypes.TEXT,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brands',
        'clientId',
        {
          type: Sequelize.DataTypes.UUID,

          references: {
            model: 'clients',
            key: 'id',
          },
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brands',
        'name',
        {
          type: Sequelize.DataTypes.TEXT,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brands',
        'rate_card_import',
        {
          type: Sequelize.DataTypes.BOOLEAN,

          defaultValue: false,
          allowNull: false,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brands',
        'client_fixed_text',
        {
          type: Sequelize.DataTypes.TEXT,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brands',
        'yes_fixed_text',
        {
          type: Sequelize.DataTypes.TEXT,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brands',
        'default_client_contact',
        {
          type: Sequelize.DataTypes.TEXT,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brands',
        'default_account_managerId',
        {
          type: Sequelize.DataTypes.UUID,

          references: {
            model: 'users',
            key: 'id',
          },
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brands',
        'default_salesId',
        {
          type: Sequelize.DataTypes.UUID,

          references: {
            model: 'users',
            key: 'id',
          },
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brands',
        'inherit_default_rate_card',
        {
          type: Sequelize.DataTypes.BOOLEAN,

          defaultValue: false,
          allowNull: false,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brands',
        'link_rate_cardId',
        {
          type: Sequelize.DataTypes.UUID,

          references: {
            model: 'brand_rate_cards',
            key: 'id',
          },
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brands',
        'default_costs',
        {
          type: Sequelize.DataTypes.DECIMAL,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brands',
        'production_fee',
        {
          type: Sequelize.DataTypes.DECIMAL,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brands',
        'insurance_fee',
        {
          type: Sequelize.DataTypes.DECIMAL,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brands',
        'temp',
        {
          type: Sequelize.DataTypes.TEXT,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brands',
        'xero_id',
        {
          type: Sequelize.DataTypes.TEXT,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brand_rate_cards',
        'name',
        {
          type: Sequelize.DataTypes.TEXT,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brand_rate_cards',
        'rate_card_type',
        {
          type: Sequelize.DataTypes.TEXT,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brand_rate_cards',
        'clientId',
        {
          type: Sequelize.DataTypes.UUID,

          references: {
            model: 'clients',
            key: 'id',
          },
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brand_rate_cards',
        'brandId',
        {
          type: Sequelize.DataTypes.UUID,

          references: {
            model: 'brands',
            key: 'id',
          },
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brand_rate_cards',
        'status',
        {
          type: Sequelize.DataTypes.TEXT,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brand_rate_cards',
        'currency',
        {
          type: Sequelize.DataTypes.TEXT,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brand_rate_cards',
        'push_new_values',
        {
          type: Sequelize.DataTypes.BOOLEAN,

          defaultValue: false,
          allowNull: false,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brand_rate_cards',
        'inherit_default_rate_card',
        {
          type: Sequelize.DataTypes.BOOLEAN,

          defaultValue: false,
          allowNull: false,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brand_rate_cards',
        'service_count',
        {
          type: Sequelize.DataTypes.INTEGER,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brand_rate_cards',
        'discipline',
        {
          type: Sequelize.DataTypes.TEXT,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brand_rate_cards',
        'output_channel',
        {
          type: Sequelize.DataTypes.TEXT,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brand_rate_cards',
        'staff_skills',
        {
          type: Sequelize.DataTypes.TEXT,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brand_rate_cards',
        'rate_card_owner',
        {
          type: Sequelize.DataTypes.TEXT,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brand_rate_cards',
        'rate_card_id',
        {
          type: Sequelize.DataTypes.TEXT,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brand_rate_cards',
        'markets',
        {
          type: Sequelize.DataTypes.TEXT,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brand_rate_cards',
        'languages',
        {
          type: Sequelize.DataTypes.TEXT,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brand_rate_cards',
        'valid_from',
        {
          type: Sequelize.DataTypes.DATEONLY,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brand_rate_cards',
        'valid_to',
        {
          type: Sequelize.DataTypes.DATEONLY,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brand_rate_cards',
        'version_number',
        {
          type: Sequelize.DataTypes.TEXT,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brand_rate_cards',
        'commercial_owner',
        {
          type: Sequelize.DataTypes.TEXT,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brand_rate_cards',
        'review_date',
        {
          type: Sequelize.DataTypes.DATEONLY,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brand_rate_cards',
        'assumptions',
        {
          type: Sequelize.DataTypes.TEXT,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brand_rate_cards',
        'no_fixed_text',
        {
          type: Sequelize.DataTypes.TEXT,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'brand_rate_cards',
        'yes_fixed_text',
        {
          type: Sequelize.DataTypes.TEXT,
        },
        { transaction },
      );

      await transaction.commit();
    } catch (err) {
      await transaction.rollback();
      throw err;
    }
  },
  /**
   * @param {QueryInterface} queryInterface
   * @param {Sequelize} Sequelize
   * @returns {Promise<void>}
   */
  async down(queryInterface, Sequelize) {
    /**
     * @type {Transaction}
     */
    const transaction = await queryInterface.sequelize.transaction();
    try {
      await queryInterface.removeColumn('brand_rate_cards', 'yes_fixed_text', {
        transaction,
      });

      await queryInterface.removeColumn('brand_rate_cards', 'no_fixed_text', {
        transaction,
      });

      await queryInterface.removeColumn('brand_rate_cards', 'assumptions', {
        transaction,
      });

      await queryInterface.removeColumn('brand_rate_cards', 'review_date', {
        transaction,
      });

      await queryInterface.removeColumn(
        'brand_rate_cards',
        'commercial_owner',
        { transaction },
      );

      await queryInterface.removeColumn('brand_rate_cards', 'version_number', {
        transaction,
      });

      await queryInterface.removeColumn('brand_rate_cards', 'valid_to', {
        transaction,
      });

      await queryInterface.removeColumn('brand_rate_cards', 'valid_from', {
        transaction,
      });

      await queryInterface.removeColumn('brand_rate_cards', 'languages', {
        transaction,
      });

      await queryInterface.removeColumn('brand_rate_cards', 'markets', {
        transaction,
      });

      await queryInterface.removeColumn('brand_rate_cards', 'rate_card_id', {
        transaction,
      });

      await queryInterface.removeColumn('brand_rate_cards', 'rate_card_owner', {
        transaction,
      });

      await queryInterface.removeColumn('brand_rate_cards', 'staff_skills', {
        transaction,
      });

      await queryInterface.removeColumn('brand_rate_cards', 'output_channel', {
        transaction,
      });

      await queryInterface.removeColumn('brand_rate_cards', 'discipline', {
        transaction,
      });

      await queryInterface.removeColumn('brand_rate_cards', 'service_count', {
        transaction,
      });

      await queryInterface.removeColumn(
        'brand_rate_cards',
        'inherit_default_rate_card',
        { transaction },
      );

      await queryInterface.removeColumn('brand_rate_cards', 'push_new_values', {
        transaction,
      });

      await queryInterface.removeColumn('brand_rate_cards', 'currency', {
        transaction,
      });

      await queryInterface.removeColumn('brand_rate_cards', 'status', {
        transaction,
      });

      await queryInterface.removeColumn('brand_rate_cards', 'brandId', {
        transaction,
      });

      await queryInterface.removeColumn('brand_rate_cards', 'clientId', {
        transaction,
      });

      await queryInterface.removeColumn('brand_rate_cards', 'rate_card_type', {
        transaction,
      });

      await queryInterface.removeColumn('brand_rate_cards', 'name', {
        transaction,
      });

      await queryInterface.removeColumn('brands', 'xero_id', { transaction });

      await queryInterface.removeColumn('brands', 'temp', { transaction });

      await queryInterface.removeColumn('brands', 'insurance_fee', {
        transaction,
      });

      await queryInterface.removeColumn('brands', 'production_fee', {
        transaction,
      });

      await queryInterface.removeColumn('brands', 'default_costs', {
        transaction,
      });

      await queryInterface.removeColumn('brands', 'link_rate_cardId', {
        transaction,
      });

      await queryInterface.removeColumn('brands', 'inherit_default_rate_card', {
        transaction,
      });

      await queryInterface.removeColumn('brands', 'default_salesId', {
        transaction,
      });

      await queryInterface.removeColumn('brands', 'default_account_managerId', {
        transaction,
      });

      await queryInterface.removeColumn('brands', 'default_client_contact', {
        transaction,
      });

      await queryInterface.removeColumn('brands', 'yes_fixed_text', {
        transaction,
      });

      await queryInterface.removeColumn('brands', 'client_fixed_text', {
        transaction,
      });

      await queryInterface.removeColumn('brands', 'rate_card_import', {
        transaction,
      });

      await queryInterface.removeColumn('brands', 'name', { transaction });

      await queryInterface.removeColumn('brands', 'clientId', { transaction });

      await queryInterface.removeColumn('organizations', 'name', {
        transaction,
      });

      await transaction.commit();
    } catch (err) {
      await transaction.rollback();
      throw err;
    }
  },
};
