import { DataTypes, Model, Optional, Sequelize } from 'sequelize';

export interface SocketAttributes extends Optional<SocketInterface, 'id'> { }

export class Socket extends Model<SocketInterface, SocketAttributes> implements SocketInterface {
    public id!: number;
    public userId!: number;
    public accountId!: number | null;
    public socketId!: string;
    public revisionId!: number;
    public status!: number;
    public readonly createdAt!: Date;
    public readonly updatedAt!: Date;

    static initModel(sequelize: Sequelize): typeof Socket {
        Socket.init(
            {
                id: { type: DataTypes.BIGINT, autoIncrement: true, primaryKey: true, comment: "unique identifier" },
                userId: { type: DataTypes.BIGINT, allowNull: true, comment: "User identifier" },
                accountId: { type: DataTypes.BIGINT, allowNull: true, defaultValue: null, comment: "User's identifier" },
                socketId: { type: DataTypes.STRING, allowNull: false, comment: "socket identifier" },
                status: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 0, comment: "setting status" },
            },
            {
                paranoid: true,
                underscored: true,
                sequelize,
                tableName: 'sockets',
                timestamps: true,
                indexes: [
                    { name: 'socket-index', fields: ['user_id','socket_id'] },
                ]
            }
        );
        return Socket;
    }
}
