<?php

declare(strict_types=1);

namespace FormConnector\Integration\Forms\Adapters;

/**
 * WPForms Adapter
 * 
 * Handles form submissions from WPForms.
 */
class WPFormsAdapter extends AbstractAdapter {

	/**
	 * Plugin identifier
	 */
	protected string $plugin_key = 'wpforms';

	/**
	 * Plugin display name
	 */
	protected string $plugin_name = 'WPForms';

	/**
	 * Check if WPForms is active
	 */
	public function is_active(): bool {
		return function_exists('wpforms');
	}

	/**
	 * Register WordPress hooks
	 */
	public function register_hooks(): void {
		if (!$this->is_active()) {
			return;
		}

		add_action('wpforms_process_complete', [$this, 'handle_submission'], 10, 4);
	}

	/**
	 * Handle WPForms submission
	 *
	 * @param array $fields Processed field values
	 * @param array $entry Entry data
	 * @param array $form_data Form configuration
	 * @param int $entry_id Entry ID
	 */
	public function handle_submission(array $fields, array $entry, array $form_data, int $entry_id): void {
		$form_id = (int) ($form_data['id'] ?? 0);
		$form_name = $form_data['settings']['form_title'] ?? "Form #{$form_id}";

		$this->log_debug('Submission received', [
			'form_id' => $form_id,
			'form_name' => $form_name,
		]);

		// Extract field values
		$field_values = $this->extract_field_values($fields, $form_data);

		// Process the submission
		$this->process_submission($form_id, $form_name, $field_values);
	}

	/**
	 * Extract field values from WPForms fields
	 *
	 * @param array $fields Processed fields
	 * @param array $form_data Form configuration
	 * @return array Field values keyed by field ID
	 */
	private function extract_field_values(array $fields, array $form_data): array {
		$values = [];

		foreach ($fields as $field_id => $field) {
			$field_type = $field['type'] ?? '';
			$value = $field['value'] ?? '';

			// Handle name fields
			if ($field_type === 'name') {
				$first = trim($field['first'] ?? '');
				$last = trim($field['last'] ?? '');
				$middle = trim($field['middle'] ?? '');
				
				$values["field_{$field_id}_first"] = $first;
				$values["field_{$field_id}_last"] = $last;
				$values["field_{$field_id}_middle"] = $middle;
				$values["field_{$field_id}"] = trim($value);
				
				// Add plain first_name/last_name for easier mapping
				if (empty($values['first_name']) && !empty($first)) {
					$values['first_name'] = $first;
				}
				if (empty($values['last_name']) && !empty($last)) {
					$values['last_name'] = $last;
				}
				continue;
			}

			// Handle address fields
			if ($field_type === 'address') {
				$values["field_{$field_id}_address1"] = trim($field['address1'] ?? '');
				$values["field_{$field_id}_address2"] = trim($field['address2'] ?? '');
				$values["field_{$field_id}_city"] = trim($field['city'] ?? '');
				$values["field_{$field_id}_state"] = trim($field['state'] ?? '');
				$values["field_{$field_id}_postal"] = trim($field['postal'] ?? '');
				$values["field_{$field_id}_country"] = trim($field['country'] ?? '');
				$values["field_{$field_id}"] = trim($value);
				continue;
			}

			// Regular fields
			if (is_array($value)) {
				$values["field_{$field_id}"] = implode(', ', array_filter(array_map('trim', $value)));
			} else {
				$values["field_{$field_id}"] = trim((string) $value);
			}
		}

		return $values;
	}
}
