const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');

const EventParticipant = sequelize.define('EventParticipant', {
  id: {
    type: DataTypes.INTEGER.UNSIGNED,
    primaryKey: true,
    autoIncrement: true,
    comment: '이벤트 참가자 고유 식별자'
  },
  eventId: {
    type: DataTypes.INTEGER.UNSIGNED,
    allowNull: false,
    references: {
      model: 'Events',
      key: 'id'
    },
    comment: '이벤트 ID'
  },
  memberId: {
    type: DataTypes.INTEGER.UNSIGNED,
    allowNull: false,
    references: {
      model: 'Members',
      key: 'id'
    },
    comment: '회원 ID'
  },
  teamId: {
    type: DataTypes.INTEGER.UNSIGNED,
    allowNull: true,
    references: {
      model: 'EventTeams',
      key: 'id'
    },
    comment: '이벤트 팀 ID'
  },
  comment: {
    type: DataTypes.STRING,
    allowNull: true,
    comment: '비고'
  },
  status: {
    type: DataTypes.ENUM('pending', 'confirmed', 'canceled'),
    allowNull: false,
    defaultValue: 'pending',
    comment: '참가 상태'
  },
  paymentStatus: {
    type: DataTypes.ENUM('unpaid', 'paid'),
    allowNull: false,
    defaultValue: 'unpaid',
    comment: '참가비 납부 상태'
  }
}, {
  tableName: 'EventParticipants',
  comment: '이벤트별 참가자 정보를 저장하는 테이블',
  indexes: [
    {
      unique: true,
      fields: ['eventId', 'memberId'],
      name: 'unique_event_member'
    }
  ],
  timestamps: true
});

// 관계 설정
EventParticipant.associate = (models) => {
  EventParticipant.belongsTo(models.Event, {
    foreignKey: 'eventId'
  });
  EventParticipant.belongsTo(models.Member, {
    foreignKey: 'memberId'
  });
  EventParticipant.belongsTo(models.EventTeam, {
    foreignKey: 'teamId'
  });
  EventParticipant.hasMany(models.EventScore, {
    foreignKey: 'participantId'
  });
};

module.exports = EventParticipant;
