import { DataTypes } from 'sequelize';
import sequelize from '../database.js';
import Club from './Club.js';

const CalendarEvent = sequelize.define('CalendarEvent', {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  clubId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    references: { model: Club, key: 'id' },
    onDelete: 'CASCADE',
  },
  eventType: {
    type: DataTypes.STRING(32),
    allowNull: false,
    defaultValue: 'club_event',
    field: 'event_type',
  },
  status: {
    type: DataTypes.STRING(32),
    allowNull: false,
    defaultValue: 'planning',
  },
  title: { type: DataTypes.STRING(255), allowNull: false },
  description: { type: DataTypes.TEXT, allowNull: true },
  location: { type: DataTypes.STRING(255), allowNull: true },
  startDate: { type: DataTypes.DATEONLY, allowNull: false, field: 'start_date' },
  endDate: { type: DataTypes.DATEONLY, allowNull: false, field: 'end_date' },
  registrationDeadline: {
    type: DataTypes.DATEONLY,
    allowNull: true,
    field: 'registration_deadline',
  },
  organizerUserId: {
    type: DataTypes.INTEGER,
    allowNull: true,
    field: 'organizer_user_id',
  },
  category: { type: DataTypes.STRING(64), allowNull: true },
  notes: { type: DataTypes.TEXT, allowNull: true },
}, {
  tableName: 'calendar_events',
  underscored: true,
  timestamps: true,
  indexes: [
    { fields: ['club_id', 'start_date'] },
    { fields: ['club_id', 'event_type'] },
    { fields: ['club_id', 'status'] },
    { fields: ['club_id', 'registration_deadline'] },
  ],
});

export default CalendarEvent;
