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

const Feature = sequelize.define('Feature', {
  id: {
    type: DataTypes.INTEGER.UNSIGNED,
    primaryKey: true,
    autoIncrement: true,
    comment: '기능 고유 식별자'
  },
  code: {
    type: DataTypes.STRING,
    allowNull: false,
    unique: true,
    comment: '기능 코드'
  },
  name: {
    type: DataTypes.STRING,
    allowNull: false,
    comment: '기능명'
  },
  description: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '기능 설명'
  },
  price: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
    comment: '기능 가격'
  },
  status: {
    type: DataTypes.ENUM('active', 'inactive', 'suspended'),
    allowNull: false,
    defaultValue: 'active',
    comment: '기능 상태'
  },
  createdAt: {
    type: DataTypes.DATE,
    allowNull: false,
    comment: '생성일'
  },
  updatedAt: {
    type: DataTypes.DATE,
    allowNull: false,
    comment: '수정일'
  }
}, {
  tableName: 'Features',
  comment: '클럽에서 제공하는 기능 정보를 저장하는 테이블',
  timestamps: true
});

Feature.associate = (models) => {
  Feature.hasMany(models.ClubFeature, { foreignKey: 'featureId' });
  Feature.belongsToMany(models.Club, {
    through: models.ClubFeature,
    foreignKey: 'featureId',
  });
  Feature.hasMany(models.Menu, {
    foreignKey: 'featureCode',
    sourceKey: 'code',
  });
  // SubscriptionPlan 관계는 이미 있음

  Feature.belongsToMany(models.SubscriptionPlan, {
    through: models.SubscriptionPlanFeature,
    foreignKey: 'featureId',
    otherKey: 'planId',
  });
};

module.exports = Feature;
