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

export interface ActivityLogAttributes extends Optional<ActivityLogInterface, 'id'> { }

export class ActivityLog extends Model<ActivityLogInterface, ActivityLogAttributes> implements ActivityLogInterface {
    public id!: number;
    public activityId!: number;
    public userId!: number;
    public accountId!: number | null;
    public ip!: string;
    public replacements!: JSON;
    public deviceInfo!: JSON;
    public location!: JSON;
    public readonly createdAt!: Date;
    public readonly updatedAt!: Date;

    static initModel(sequelize: Sequelize): typeof ActivityLog {
        ActivityLog.init(
            {
                id: { type: DataTypes.BIGINT, autoIncrement: true, primaryKey: true, comment: "unique identifier" },
                activityId: { type: DataTypes.BIGINT, allowNull: false, comment: "activity identifier" },
                userId: { type: DataTypes.BIGINT, allowNull: false, comment: "activity user identifier" },
                accountId: { type: DataTypes.BIGINT, allowNull: true, defaultValue: null, comment: "activity user account identifier" },
                ip: { type: DataTypes.STRING, allowNull: false, comment: "activity title" },
                replacements: { type: DataTypes.JSON, allowNull: true, defaultValue: null, comment: "activity replacements" },
                deviceInfo: { type: DataTypes.JSON, allowNull: true, defaultValue: null, comment: "device info" },
                location: { type: DataTypes.JSON, allowNull: true, defaultValue: null, comment: "activity Location data" },
            },
            {
                paranoid: true,
                underscored: true,
                sequelize,
                tableName: 'activity_logs',
                timestamps: true,
                indexes: [
                    { name: 'activity-log-user', fields: ['user_id'] },
                    { name: 'activity-log-account', fields: ['account_id'] }
                ]
            }
        );
        return ActivityLog;
    }

    public static associate(models: any) {
        ActivityLog.belongsTo(models.Language, { foreignKey: 'languageId', as: 'language' });
        ActivityLog.belongsTo(models.Activity, { foreignKey: 'activityId', as: 'activity' });
    }
}
