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

export interface WebhookDumpInterface {
    id: number;
    data: object;
    type: string;
    createdAt?: Date;
    updatedAt?: Date;
}

export interface WebhookDumpAttributes extends Optional<WebhookDumpInterface, 'id'> { }

export class WebhookDump extends Model<WebhookDumpInterface, WebhookDumpAttributes> implements WebhookDumpInterface {
    public id!: number;
    public data!: object;
    public type!: string;
    public readonly createdAt!: Date;
    public readonly updatedAt!: Date;

    static initModel(sequelize: Sequelize): typeof WebhookDump {
        WebhookDump.init(
            {
                id: {
                    type: DataTypes.BIGINT,
                    autoIncrement: true,
                    primaryKey: true,
                    comment: "Unique identifier for webhook dump"
                },
                data: {
                    type: DataTypes.JSON,
                    allowNull: true,
                    comment: "Full webhook payload"
                },
                type: {
                    type: DataTypes.STRING,
                    allowNull: true,
                    comment: "Webhook event type (e.g. 'payment_success', 'subscription_created')"
                },
            },
            {
                paranoid: true,
                underscored: true,
                sequelize,
                tableName: 'webhook_dumps',
                timestamps: true,
                indexes: [
                    { name: 'webhook-type-index', fields: ['type'] },
                    { name: 'webhook-createdat-index', fields: ['created_at'] },
                ]
            }
        );
        return WebhookDump;
    }

    public static associate(models: any) {
        // If you ever want to relate WebhookDump to other models, do it here
        // Example:
        // WebhookDump.belongsTo(models.Account, { foreignKey: 'accountId', as: 'account' });
    }
}
