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

const Event = sequelize.define('Event', {
  id: {
    type: DataTypes.INTEGER.UNSIGNED,
    primaryKey: true,
    autoIncrement: true,
    comment: '이벤트 고유 식별자'
  },
  clubId: {
    type: DataTypes.INTEGER.UNSIGNED,
    allowNull: false,
    references: {
      model: 'Clubs',
      key: 'id'
    },
    comment: '소속 클럽 ID'
  },
  title: {
    type: DataTypes.STRING,
    allowNull: false,
    comment: '이벤트 제목'
  },
  description: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '이벤트 설명'
  },
  eventType: {
    type: DataTypes.ENUM('regular', 'lightning', 'practice', 'exchange', 'event', 'other'),
    allowNull: false,
    defaultValue: 'regular',
    comment: '이벤트 유형'
  },
  publicHash: {
    type: DataTypes.STRING,
    allowNull: true,
    comment: '공개 URL 해시'
  },
  accessPassword: {
    type: DataTypes.STRING,
    allowNull: true,
    comment: '공개 URL 비밀번호'
  },
  startDate: {
    type: DataTypes.DATE,
    allowNull: false,
    comment: '시작일'
  },
  endDate: {
    type: DataTypes.DATE,
    allowNull: true,
    comment: '종료일'
  },
  location: {
    type: DataTypes.STRING,
    allowNull: true,
    comment: '장소'
  },
  gameCount: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 3,
    comment: '게임 수'
  },
  participantFee: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: true,
    defaultValue: null,
    comment: '참가비'
  },
  maxParticipants: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
    comment: '최대 참가 인원'
  },
  registrationDeadline: {
    type: DataTypes.DATE,
    allowNull: true,
    comment: '참가 신청 마감일'
  },
  participantCount: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
    comment: '참가자 수'
  },
  status: {
    type: DataTypes.ENUM('ready', 'active', 'completed', 'canceled', 'deleted'),
    allowNull: false,
    defaultValue: 'active',
    comment: '이벤트 상태'
  },
  createdAt: {
    type: DataTypes.DATE,
    allowNull: false,
    comment: '생성일'
  },
  updatedAt: {
    type: DataTypes.DATE,
    allowNull: false,
    comment: '수정일'
  }
}, {
  tableName: 'Events',
  comment: '클럽 내 이벤트(대회, 모임 등) 정보를 저장하는 테이블',
  timestamps: true
});

// 관계 설정
Event.associate = (models) => {
  Event.belongsTo(models.Club, {
    foreignKey: 'clubId'
  });
  
  Event.hasMany(models.EventParticipant, {
    foreignKey: 'eventId'
  });

  Event.hasMany(models.EventScore, {
    foreignKey: 'eventId'
  });

  // 팀 연관관계 추가
  Event.hasMany(models.EventTeam, {
    foreignKey: 'eventId'
  });
};

module.exports = Event;
