<?php

namespace WPDRMS\ASP\AI;

use WPDRMS\ASP\AI\ORM\AIInteraction;
use WPDRMS\PluginCore\Interfaces\Registrable;

if ( !defined( 'ABSPATH' ) ) {
	die( '-1' );
}

/**
 * Registers AI-domain WordPress hooks. Currently schedules the daily prune that
 * keeps the AIInteraction log table bounded.
 */
class AIHooks implements Registrable {
	public const PRUNE_HOOK = 'asp_ai_prune_interactions';

	/**
	 * Interaction-log retention window in days.
	 */
	private const RETENTION_DAYS = 90;

	public function register(): void {
		if ( ! wp_next_scheduled( self::PRUNE_HOOK ) ) {
			wp_schedule_event( time(), 'daily', self::PRUNE_HOOK );
		}
		add_action( self::PRUNE_HOOK, array( $this, 'pruneInteractions' ) );
	}

	public function deregister(): void {
		wp_clear_scheduled_hook( self::PRUNE_HOOK );
		remove_action( self::PRUNE_HOOK, array( $this, 'pruneInteractions' ) );
	}

	public function pruneInteractions(): void {
		AIInteraction::prune( self::RETENTION_DAYS );
	}
}
