<?php
if ( ! defined( 'ABSPATH' ) ) {
	die( 'You are not allowed to call this page directly.' );
}

class FormShieldIntegration {

	public static $form_prefix = '';

	/*
	 * This is where you will add filters and actions
	 */
	public function __construct() {

	}

	public static function get_form_id( $id ) {
		return static::$form_prefix . '-' . $id;
	}

	/*
	 * This should return the forms list
	 *
	 * @return Array $forms_list
	 */
	public static function get_forms( $forms_list ) {

	}

	/*
	 * This is for the autoprotect process and should enable all forms for validation.
	 * This should be added to the formshield_autoprotect_forms action.
	 * This should implement the FormShieldForms::enable_forms() static method to enable each form
	 */
	public static function enable_forms( $unattended ) {

	}

	public static function prune_disabled_forms( array $forms_list ) {
		$options = form_shield()->get_options();
		$changed = false;
		if ( ! isset( $options['formshield_forms'] ) || empty( $options['formshield_forms'] ) ) {
			return;
		}

		// Loop through all the forms registered with HP to make sure they still exist in the form list provided by the plugin
		$forms = array_map( function ( $f ) {
			return $f['id'];
		}, $forms_list );
		foreach ( $options['formshield_forms'] as $fid => $formshield_form ) {
			// The $forms_list only contains forms from 1 integration at a time, so to be sure that we don't remove
			// forms that aren't in this integration, ensure they have the same prefix as the current integration
			// before checking if they're in the formshield_forms list.
			if ( FormShieldUtils::startsWith( $fid, static::$form_prefix ) && ! in_array( $fid, $forms ) ) {
				if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
					error_log(
						sprintf(
							'[FormShield DEBUG] prune_disabled_forms removing id=%s prefix=%s available_ids=%s',
							(string) $fid,
							(string) static::$form_prefix,
							implode( ',', $forms )
						)
					);
				}
				$changed = true;
				unset( $options['formshield_forms'][ $fid ] );
			}
		}

		// Now save the new filtered set of options
		if ( $changed ) {
			form_shield()->update_options( $options );
		}
	}
}
