import { DataTypes, Model, Optional } from 'sequelize';
import { sequelize } from '../config/database';

interface ChatRoomAttributes {
  id: number;
  name: string;
  description?: string;
  type: 'group' | 'direct';
  created_by: string;
  createdAt?: Date;
  updatedAt?: Date;
}

interface ChatRoomCreationAttributes extends Optional<ChatRoomAttributes, 'id' | 'createdAt' | 'updatedAt'> {}

class ChatRoom extends Model<ChatRoomAttributes, ChatRoomCreationAttributes> implements ChatRoomAttributes {
  public id!: number;
  public name!: string;
  public description?: string;
  public type!: 'group' | 'direct';
  public created_by!: string;
  public readonly createdAt!: Date;
  public readonly updatedAt!: Date;
}

ChatRoom.init(
  {
    id: {
      type: DataTypes.INTEGER.UNSIGNED,
      autoIncrement: true,
      primaryKey: true,
    },
    name: {
      type: DataTypes.STRING(255),
      allowNull: false,
    },
    description: {
      type: DataTypes.TEXT,
      allowNull: true,
    },
    type: {
      type: DataTypes.ENUM('group', 'direct'),
      allowNull: false,
      defaultValue: 'group',
    },
    created_by: {
      type: DataTypes.STRING(50),
      allowNull: false,
      references: {
        model: 'users',
        key: 'user_id',
      },
    },
  },
  {
    sequelize,
    tableName: 'chat_rooms',
    timestamps: true,
  }
);

// Relationships will be defined in models/index.ts to avoid circular dependencies

export default ChatRoom;
