const User = require('./User');
const ChitGroup = require('./ChitGroup');
const GroupMember = require('./GroupMember');
const Payment = require('./Payment');
const MonthlyDraw = require('./MonthlyDraw');
const Notification = require('./Notification');

// User associations
User.hasMany(ChitGroup, { as: 'managedGroups', foreignKey: 'manager_id' });
User.hasMany(GroupMember, { foreignKey: 'user_id' });
User.hasMany(Payment, { foreignKey: 'user_id' });
User.hasMany(MonthlyDraw, { as: 'wins', foreignKey: 'winner_id' });
User.belongsTo(User, { as: 'creator', foreignKey: 'created_by' });

// ChitGroup associations
ChitGroup.belongsTo(User, { as: 'manager', foreignKey: 'manager_id' });
ChitGroup.hasMany(GroupMember, { as: 'members', foreignKey: 'group_id' });
ChitGroup.hasMany(Payment, { foreignKey: 'group_id' });
ChitGroup.hasMany(MonthlyDraw, { foreignKey: 'group_id' });

// GroupMember associations
GroupMember.belongsTo(User, { foreignKey: 'user_id' });
GroupMember.belongsTo(ChitGroup, { foreignKey: 'group_id' });

// Payment associations
Payment.belongsTo(User, { foreignKey: 'user_id' });
Payment.belongsTo(ChitGroup, { foreignKey: 'group_id' });

// MonthlyDraw associations
MonthlyDraw.belongsTo(ChitGroup, { foreignKey: 'group_id' });
MonthlyDraw.belongsTo(User, { as: 'winner', foreignKey: 'winner_id' });

// Notification associations
Notification.belongsTo(User, { foreignKey: 'user_id' });
Notification.belongsTo(ChitGroup, { as: 'group', foreignKey: 'group_id' });
Notification.belongsTo(Payment, { foreignKey: 'payment_id' });
Notification.belongsTo(MonthlyDraw, { foreignKey: 'draw_id' });

// User has many notifications
User.hasMany(Notification, { foreignKey: 'user_id' });

module.exports = {
  User,
  ChitGroup,
  GroupMember,
  Payment,
  MonthlyDraw,
  Notification
};
