'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('notification', {
      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: 'RESTRICT'
      },
      user_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
        comment: 'User who will receive the notification'
      },
      type: {
        type: Sequelize.ENUM('APPROVAL_REQUEST', 'APPROVAL_APPROVED', 'APPROVAL_REJECTED', 'SYSTEM', 'INFO', 'WARNING', 'ERROR'),
        allowNull: false,
        defaultValue: 'INFO'
      },
      module_name: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Module name (e.g., PurchaseOrder, SalesOrder)'
      },
      transaction_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: 'ID of the transaction (e.g., PO ID, SO ID)'
      },
      transaction_number: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Transaction number for reference'
      },
      title: {
        type: Sequelize.STRING(200),
        allowNull: false
      },
      message: {
        type: Sequelize.TEXT,
        allowNull: false
      },
      link: {
        type: Sequelize.STRING(500),
        allowNull: true,
        comment: 'URL to navigate when notification is clicked'
      },
      is_read: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: false
      },
      read_at: {
        type: Sequelize.DATE,
        allowNull: true
      },
      priority: {
        type: Sequelize.ENUM('LOW', 'MEDIUM', 'HIGH', 'URGENT'),
        allowNull: false,
        defaultValue: 'MEDIUM'
      },
      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 ON UPDATE CURRENT_TIMESTAMP')
      }
    });

    // Add indexes
    await queryInterface.addIndex('notification', ['user_id', 'is_read'], {
      name: 'idx_notification_user_read'
    });
    await queryInterface.addIndex('notification', ['company_id', 'user_id'], {
      name: 'idx_notification_company_user'
    });
    await queryInterface.addIndex('notification', ['type', 'module_name'], {
      name: 'idx_notification_type_module'
    });
    await queryInterface.addIndex('notification', ['created_at'], {
      name: 'idx_notification_created_at'
    });
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('notification');
  }
};

