import * as Sequelize from 'sequelize';
import { DataTypes, Model, Optional } from 'sequelize';
import type { Resident, ResidentId } from './Resident';
export interface WebhookLogAttributes {
	id: string;
	event_id?: string | null;
	webhook_type: string;
	status: 'PENDING' | 'PROCESSING' | 'SUCCESS' | 'FAILED';
	payload?: object | null;
	response_data?: object | null;
	error_message?: string | null;
	resident_id?: string | null;
	ip_address?: string | null;
	user_agent?: string | null;
	retry_count?: number | null;
	processed_at?: Date | null;
	failed_at?: Date | null;
	created_at: Date;
	updated_at: Date;
}
export type WebhookLogPk = 'id';
export type WebhookLogId = WebhookLog[WebhookLogPk];
export type WebhookLogOptionalAttributes =
	| 'id'
	| 'event_id'
	| 'status'
	| 'payload'
	| 'response_data'
	| 'error_message'
	| 'resident_id'
	| 'ip_address'
	| 'user_agent'
	| 'retry_count'
	| 'processed_at'
	| 'failed_at'
	| 'created_at'
	| 'updated_at';
export type WebhookLogCreationAttributes = Optional<WebhookLogAttributes, WebhookLogOptionalAttributes>;
export class WebhookLog extends Model<WebhookLogAttributes> implements WebhookLogAttributes {
	declare id: string;
	declare event_id?: string | null;
	declare webhook_type: string;
	declare status: 'PENDING' | 'PROCESSING' | 'SUCCESS' | 'FAILED';
	declare payload?: object | null;
	declare response_data?: object | null;
	declare error_message?: string | null;
	declare resident_id?: string | null;
	declare ip_address?: string | null;
	declare user_agent?: string | null;
	declare retry_count?: number | null;
	declare processed_at?: Date | null;
	declare failed_at?: Date | null;
	declare created_at: Date;
	declare updated_at: Date;
	// WebhookLog belongsTo Resident via resident_id
	declare resident: Resident;
	declare getResident: Sequelize.BelongsToGetAssociationMixin<Resident>;
	declare setResident: Sequelize.BelongsToSetAssociationMixin<Resident, ResidentId>;
	declare createResident: Sequelize.BelongsToCreateAssociationMixin<Resident>;
	static initModel(sequelize: Sequelize.Sequelize): typeof WebhookLog {
		return WebhookLog.init(
			{
				id: {
					type: DataTypes.UUID,
					allowNull: false,
					defaultValue: DataTypes.UUIDV4,
					primaryKey: true,
				},
				event_id: {
					type: DataTypes.STRING(255),
					allowNull: true,
					comment: 'Event ID from partner system for tracking and reconciliation',
				},
				webhook_type: {
					type: DataTypes.STRING(50),
					allowNull: false,
					comment: 'Type of webhook (e.g., RESIDENT_KTX, RESIDENT_TRO)',
				},
				status: {
					type: DataTypes.ENUM('PENDING', 'PROCESSING', 'SUCCESS', 'FAILED'),
					allowNull: false,
					defaultValue: 'PROCESSING',
					comment: 'Processing status: PENDING, PROCESSING, SUCCESS, FAILED',
				},
				payload: {
					type: DataTypes.JSONB,
					allowNull: true,
					comment: 'Original webhook payload received from partner',
				},
				response_data: {
					type: DataTypes.JSONB,
					allowNull: true,
					comment: 'Response data sent back to partner',
				},
				error_message: {
					type: DataTypes.TEXT,
					allowNull: true,
					comment: 'Error message if processing failed',
				},
				resident_id: {
					type: DataTypes.UUID,
					allowNull: true,
					comment: 'ID of the resident created\/updated by this webhook',
					references: {
						model: 'residents',
						key: 'id',
					},
				},
				ip_address: {
					type: DataTypes.INET,
					allowNull: true,
					comment: 'IP address of the webhook sender',
				},
				user_agent: {
					type: DataTypes.TEXT,
					allowNull: true,
					comment: 'User agent of the webhook sender',
				},
				retry_count: {
					type: DataTypes.INTEGER,
					allowNull: true,
					defaultValue: 0,
					comment: 'Number of retry attempts',
				},
				processed_at: {
					type: DataTypes.DATE,
					allowNull: true,
					comment: 'Timestamp when processing completed successfully',
				},
				failed_at: {
					type: DataTypes.DATE,
					allowNull: true,
					comment: 'Timestamp when processing failed',
				},
				created_at: {
					type: DataTypes.DATE,
					allowNull: false,
					defaultValue: Sequelize.Sequelize.literal('CURRENT_TIMESTAMP'),
				},
				updated_at: {
					type: DataTypes.DATE,
					allowNull: false,
					defaultValue: Sequelize.Sequelize.literal('CURRENT_TIMESTAMP'),
				},
			},
			{
				sequelize,
				tableName: 'webhook_logs',
				schema: 'public',
				hasTrigger: true,
				timestamps: false,
				indexes: [
					{
						name: 'idx_webhook_logs_created_at',
						fields: [{ name: 'created_at', order: 'DESC' }],
					},
					{
						name: 'idx_webhook_logs_event_id',
						fields: [{ name: 'event_id' }],
					},
					{
						name: 'idx_webhook_logs_resident_id',
						fields: [{ name: 'resident_id' }],
					},
					{
						name: 'idx_webhook_logs_status',
						fields: [{ name: 'status' }],
					},
					{
						name: 'idx_webhook_logs_webhook_type',
						fields: [{ name: 'webhook_type' }],
					},
					{
						name: 'webhook_logs_pkey',
						unique: true,
						fields: [{ name: 'id' }],
					},
				],
			},
		);
	}
}
