'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('notification_setting', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        allowNull: false
      },
      company_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'company',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      auto_email_po: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: false
      },
      manual_email_po: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: true
      },
      auto_whatsapp_po: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: false
      },
      manual_whatsapp_po: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: true
      },
      email_subject_template: {
        type: Sequelize.STRING(255),
        allowNull: true
      },
      email_body_template: {
        type: Sequelize.TEXT('long'),
        allowNull: true
      },
      whatsapp_message_template: {
        type: Sequelize.TEXT('long'),
        allowNull: true
      },
      smtp_host: {
        type: Sequelize.STRING(120),
        allowNull: true
      },
      smtp_port: {
        type: Sequelize.INTEGER,
        allowNull: true
      },
      smtp_secure: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: false
      },
      smtp_user: {
        type: Sequelize.STRING(200),
        allowNull: true
      },
      smtp_password: {
        type: Sequelize.STRING(255),
        allowNull: true
      },
      smtp_from_email: {
        type: Sequelize.STRING(200),
        allowNull: true
      },
      smtp_from_name: {
        type: Sequelize.STRING(150),
        allowNull: true
      },
      whatsapp_api_url: {
        type: Sequelize.STRING(500),
        allowNull: true
      },
      whatsapp_api_token: {
        type: Sequelize.STRING(500),
        allowNull: true
      },
      status: {
        type: Sequelize.ENUM('ACTIVE', 'INACTIVE'),
        allowNull: false,
        defaultValue: 'ACTIVE'
      },
      created_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      updated_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      }
    });

    await queryInterface.addIndex('notification_setting', ['company_id'], {
      unique: true,
      name: 'notification_setting_company_id_unique'
    });
    await queryInterface.addIndex('notification_setting', ['status'], {
      name: 'notification_setting_status_idx'
    });
  },

  async down(queryInterface) {
    await queryInterface.removeIndex('notification_setting', 'notification_setting_status_idx');
    await queryInterface.removeIndex('notification_setting', 'notification_setting_company_id_unique');
    await queryInterface.dropTable('notification_setting');
  }
};
