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

/**
 * BookingRule Model
 * 预约登记规则配置，每个服务类型单独配置
 */
const BookingRule = sequelize.define('BookingRule', {
  id: {
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV4,
    primaryKey: true,
  },
  serviceType: {
    type: DataTypes.STRING(50),
    allowNull: false,
    unique: true,
    field: 'service_type',
    comment: '服务类型标识',
  },
  rules: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '预约登记规则内容',
  },
  status: {
    type: DataTypes.ENUM('active', 'inactive'),
    defaultValue: 'active',
    allowNull: false,
    comment: '状态',
  },
}, {
  tableName: 'booking_rules',
  timestamps: true,
  underscored: true,
  indexes: [
    { fields: ['service_type'], unique: true },
    { fields: ['status'] },
  ],
});

// 自动同步表结构
(async () => {
  try {
    await BookingRule.sync();
    console.log('BookingRule table synced successfully');
  } catch (err) {
    console.error('BookingRule sync error:', err.message);
  }
})();

module.exports = BookingRule;
