import { AuditStrategy, SystemAuditParams, EntityChangeParams } from './auditStrategyService';
import { AuditOutbox } from '#models/AuditOutbox';
import { randomUUID } from 'crypto';

export class OutboxAuditStrategy implements AuditStrategy {
	async logSystemAudit(params: SystemAuditParams): Promise<void> {
		await AuditOutbox.create({
			id: randomUUID(),
			request_id: randomUUID(),
			trace_id: randomUUID(),
			event_type: 'SYSTEM_AUDIT',
			payload: {
				action: params.action,
				module: params.module,
				actor_name: params.actor_name,
				actor_id: params.actor_id,
				severity: params.severity || 'LOW',
				metadata: params.metadata,
			},
			payload_size: JSON.stringify(params.metadata || {}).length,
			status: 'PENDING',
			created_at: new Date(),
			updated_at: new Date(),
			retry_count: 0,
			max_retries: 3,
		});
	}

	async logEntityChange(params: EntityChangeParams): Promise<void> {
		await AuditOutbox.create({
			id: randomUUID(),
			request_id: randomUUID(),
			trace_id: randomUUID(),
			event_type: 'ENTITY_CHANGE',
			payload: {
				entity_type: params.entity_type,
				entity_id: params.entity_id,
				change_type: params.change_type,
				actor_name: params.actor_name,
				actor_id: params.actor_id,
				actor_email: params.actor_email,
				changes: params.changes,
			},
			payload_size: JSON.stringify(params.changes || {}).length,
			status: 'PENDING',
			created_at: new Date(),
			updated_at: new Date(),
			retry_count: 0,
			max_retries: 3,
		});
	}
}
