module.exports = (sequelize, DataTypes) => {
  const ApprovalTransaction = sequelize.define('ApprovalTransaction', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      }
    },
    approval_master_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'ApprovalMaster',
        key: 'id'
      }
    },
    module_name: {
      type: DataTypes.STRING(50),
      allowNull: false,
      comment: 'Module name: PURCHASE_ORDER, SALES_ORDER, etc.'
    },
    transaction_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: 'ID of the transaction being approved (PO ID, SO ID, etc.)'
    },
    transaction_number: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'Transaction number for reference'
    },
    approval_level: {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: 'Current approval level'
    },
    status: {
      type: DataTypes.ENUM('PENDING', 'APPROVED', 'REJECTED', 'CANCELLED'),
      allowNull: false,
      defaultValue: 'PENDING'
    },
    approver_role_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Role',
        key: 'id'
      }
    },
    approver_user_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      },
      comment: 'User who approved/rejected'
    },
    approved_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    rejection_reason: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    comments: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    transaction_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: true,
      comment: 'Amount of the transaction for reference'
    },
    created_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    updated_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    }
  }, {
    tableName: 'approval_transaction',
    indexes: [
      {
        fields: ['module_name', 'transaction_id', 'status']
      },
      {
        fields: ['approval_master_id']
      },
      {
        fields: ['approver_user_id']
      },
      {
        fields: ['status']
      },
      {
        fields: ['created_by']
      }
    ]
  });

  ApprovalTransaction.associate = function(models) {
    ApprovalTransaction.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    ApprovalTransaction.belongsTo(models.ApprovalMaster, { foreignKey: 'approval_master_id', as: 'approvalMaster' });
    ApprovalTransaction.belongsTo(models.Role, { foreignKey: 'approver_role_id', as: 'approverRole' });
    ApprovalTransaction.belongsTo(models.User, { foreignKey: 'approver_user_id', as: 'approver' });
    ApprovalTransaction.belongsTo(models.User, { foreignKey: 'created_by', as: 'creator' });
    ApprovalTransaction.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updater' });
  };

  return ApprovalTransaction;
};

