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

/**
 * Category Model
 * Represents service categories (airport, train, hotel, etc.)
 */
const Category = sequelize.define('Category', {
  id: {
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV4,
    primaryKey: true,
  },
  key: {
    type: DataTypes.STRING(50),
    allowNull: false,
    unique: true,
  },
  nameZh: {
    type: DataTypes.STRING(100),
    allowNull: false,
    field: 'name_zh',
  },
  nameEn: {
    type: DataTypes.STRING(100),
    allowNull: false,
    field: 'name_en',
  },
  nameEs: {
    type: DataTypes.STRING(100),
    allowNull: false,
    field: 'name_es',
  },
  icon: {
    type: DataTypes.STRING(500),
    allowNull: true,
  },
  sortOrder: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    allowNull: false,
    field: 'sort_order',
  },
}, {
  tableName: 'category',
  timestamps: true,
  underscored: true,
  indexes: [
    { fields: ['key'] },
    { fields: ['sort_order'] },
  ],
});

// Override toJSON to return both snake_case and camelCase for compatibility
Category.prototype.toJSON = function () {
  const values = Object.assign({}, this.get());
  return {
    id: values.id,
    key: values.key,
    // snake_case (for backward compatibility)
    name_zh: values.nameZh,
    name_en: values.nameEn,
    name_es: values.nameEs,
    sort_order: values.sortOrder,
    created_at: values.createdAt,
    updated_at: values.updatedAt,
    // camelCase (for consistency)
    nameZh: values.nameZh,
    nameEn: values.nameEn,
    nameEs: values.nameEs,
    icon: values.icon,
    sortOrder: values.sortOrder,
    createdAt: values.createdAt,
    updatedAt: values.updatedAt,
  };
};

module.exports = Category;
