import * as Sequelize from 'sequelize';
import { DataTypes, Model, Optional } from 'sequelize';
import type { NotificationDevice, NotificationDeviceId } from './NotificationDevice';
import type { NotificationReceipt, NotificationReceiptId } from './NotificationReceipt';
export interface NotificationDeliveryAttemptAttributes {
	id: string;
	receipt_id: string;
	device_id?: string | null;
	channel: 'in_app' | 'push' | 'email' | 'sms' | 'zalo';
	status: 'queued' | 'processing' | 'sent' | 'delivered' | 'failed' | 'cancelled' | 'expired';
	attempt_no?: number | null;
	provider_name?: string | null;
	provider_message_id?: string | null;
	latency_ms?: number | null;
	response_payload?: object | null;
	error_message?: string | null;
	sent_at?: Date | null;
	delivered_at?: Date | null;
	failed_at?: Date | null;
	next_retry_at?: Date | null;
	created_at: Date;
	updated_at?: Date | null;
}
export type NotificationDeliveryAttemptPk = 'id' | 'created_at';
export type NotificationDeliveryAttemptId = NotificationDeliveryAttempt[NotificationDeliveryAttemptPk];
export type NotificationDeliveryAttemptOptionalAttributes =
	| 'id'
	| 'device_id'
	| 'attempt_no'
	| 'provider_name'
	| 'provider_message_id'
	| 'latency_ms'
	| 'response_payload'
	| 'error_message'
	| 'sent_at'
	| 'delivered_at'
	| 'failed_at'
	| 'next_retry_at'
	| 'created_at'
	| 'updated_at';
export type NotificationDeliveryAttemptCreationAttributes = Optional<
	NotificationDeliveryAttemptAttributes,
	NotificationDeliveryAttemptOptionalAttributes
>;
export class NotificationDeliveryAttempt
	extends Model<NotificationDeliveryAttemptAttributes>
	implements NotificationDeliveryAttemptAttributes
{
	declare id: string;
	declare receipt_id: string;
	declare device_id?: string | null;
	declare channel: 'in_app' | 'push' | 'email' | 'sms' | 'zalo';
	declare status: 'queued' | 'processing' | 'sent' | 'delivered' | 'failed' | 'cancelled' | 'expired';
	declare attempt_no?: number | null;
	declare provider_name?: string | null;
	declare provider_message_id?: string | null;
	declare latency_ms?: number | null;
	declare response_payload?: object | null;
	declare error_message?: string | null;
	declare sent_at?: Date | null;
	declare delivered_at?: Date | null;
	declare failed_at?: Date | null;
	declare next_retry_at?: Date | null;
	declare created_at: Date;
	declare updated_at?: Date | null;
	// NotificationDeliveryAttempt belongsTo NotificationDevice via device_id
	declare device: NotificationDevice;
	declare getDevice: Sequelize.BelongsToGetAssociationMixin<NotificationDevice>;
	declare setDevice: Sequelize.BelongsToSetAssociationMixin<NotificationDevice, NotificationDeviceId>;
	declare createDevice: Sequelize.BelongsToCreateAssociationMixin<NotificationDevice>;
	// NotificationDeliveryAttempt belongsTo NotificationReceipt via receipt_id
	declare receipt: NotificationReceipt;
	declare getReceipt: Sequelize.BelongsToGetAssociationMixin<NotificationReceipt>;
	declare setReceipt: Sequelize.BelongsToSetAssociationMixin<NotificationReceipt, NotificationReceiptId>;
	declare createReceipt: Sequelize.BelongsToCreateAssociationMixin<NotificationReceipt>;
	static initModel(sequelize: Sequelize.Sequelize): typeof NotificationDeliveryAttempt {
		return NotificationDeliveryAttempt.init(
			{
				id: {
					type: DataTypes.UUID,
					allowNull: false,
					defaultValue: DataTypes.UUIDV4,
					primaryKey: true,
				},
				receipt_id: {
					type: DataTypes.UUID,
					allowNull: false,
					references: {
						model: 'notification_receipts',
						key: 'id',
					},
				},
				device_id: {
					type: DataTypes.UUID,
					allowNull: true,
					references: {
						model: 'notification_devices',
						key: 'id',
					},
				},
				channel: {
					type: DataTypes.ENUM('in_app', 'push', 'email', 'sms', 'zalo'),
					allowNull: false,
				},
				status: {
					type: DataTypes.ENUM('queued', 'processing', 'sent', 'delivered', 'failed', 'cancelled', 'expired'),
					allowNull: false,
				},
				attempt_no: {
					type: DataTypes.INTEGER,
					allowNull: true,
					defaultValue: 1,
				},
				provider_name: {
					type: DataTypes.STRING(50),
					allowNull: true,
				},
				provider_message_id: {
					type: DataTypes.STRING(255),
					allowNull: true,
				},
				latency_ms: {
					type: DataTypes.INTEGER,
					allowNull: true,
				},
				response_payload: {
					type: DataTypes.JSONB,
					allowNull: true,
				},
				error_message: {
					type: DataTypes.TEXT,
					allowNull: true,
				},
				sent_at: {
					type: DataTypes.DATE,
					allowNull: true,
				},
				delivered_at: {
					type: DataTypes.DATE,
					allowNull: true,
				},
				failed_at: {
					type: DataTypes.DATE,
					allowNull: true,
				},
				next_retry_at: {
					type: DataTypes.DATE,
					allowNull: true,
				},
				created_at: {
					type: DataTypes.DATE,
					allowNull: false,
					defaultValue: Sequelize.Sequelize.literal('CURRENT_TIMESTAMP'),
					primaryKey: true,
				},
				updated_at: {
					type: DataTypes.DATE,
					allowNull: true,
					defaultValue: Sequelize.Sequelize.literal('CURRENT_TIMESTAMP'),
				},
			},
			{
				sequelize,
				tableName: 'notification_delivery_attempts',
				schema: 'public',
				timestamps: false,
			},
		);
	}
}
