'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('permission', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      permission_code: {
        type: Sequelize.STRING(100),
        allowNull: false,
        unique: true
      },
      permission_name: {
        type: Sequelize.STRING(200),
        allowNull: false
      },
      module: {
        type: Sequelize.STRING(50),
        allowNull: false,
        comment: 'Module name (e.g., MASTER, PURCHASE, SALES, PRODUCTION, FINANCE)'
      },
      resource: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: 'Resource name (e.g., material, purchase_order)'
      },
      action: {
        type: Sequelize.ENUM('CREATE', 'READ', 'UPDATE', 'DELETE', 'APPROVE', 'REJECT', 'EXPORT', 'IMPORT', 'ALL'),
        allowNull: false
      },
      description: {
        type: Sequelize.STRING(500),
        allowNull: true
      },
      is_system_permission: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: false
      },
      status: {
        type: Sequelize.ENUM('ACTIVE', 'INACTIVE'),
        allowNull: false,
        defaultValue: 'ACTIVE'
      },
      created_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      updated_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
      }
    });

    await queryInterface.addIndex('permission', ['permission_code'], { unique: true });
    await queryInterface.addIndex('permission', ['module']);
    await queryInterface.addIndex('permission', ['resource']);
    await queryInterface.addIndex('permission', ['status']);
  },

  async down(queryInterface, Sequelize) {
    await queryInterface.dropTable('permission');
  }
};

