<?php
/**
 * Ninja Forms source adapter.
 *
 * @package Leadite
 */

namespace Leadite\Capture;

use Leadite\Storage\Lead;

defined( 'ABSPATH' ) || exit;

class NinjaFormsAdapter implements SourceAdapter {

	public function slug() {
		return 'ninjaforms';
	}

	public function label() {
		return 'Ninja Forms';
	}

	public function is_available() {
		return function_exists( 'Ninja_Forms' );
	}

	public function register() {
		// Runs when submission actions are processed
		add_action( 'ninja_forms_submission_actions_processed', array( $this, 'capture' ) );
	}

	/**
	 * Capture hook: assemble raw payload, normalize, store.
	 *
	 * @param array $session_data Submission session data.
	 */
	public function capture( $session_data ) {
		$form_id = isset( $session_data['form_id'] ) ? $session_data['form_id'] : 0;
		if ( ! $form_id ) {
			return;
		}

		$form_title = '';
		if ( function_exists( 'Ninja_Forms' ) ) {
			$form = Ninja_Forms()->form( $form_id );
			if ( $form ) {
				$form_title = $form->get_setting( 'title' );
			}
		}

		$posted_fields = isset( $session_data['fields'] ) ? $session_data['fields'] : array();

		$fields = array();
		if ( function_exists( 'Ninja_Forms' ) ) {
			foreach ( $posted_fields as $field_id => $value ) {
				$field_obj = Ninja_Forms()->form( $form_id )->get_field( $field_id );
				if ( $field_obj ) {
					$fields[] = array(
						'key'   => $field_obj->get_setting( 'key' ),
						'label' => $field_obj->get_setting( 'label' ),
						'type'  => $field_obj->get_setting( 'type' ),
						'value' => $value,
					);
				} else {
					$fields[] = array(
						'key'   => $field_id,
						'label' => $field_id,
						'type'  => 'text',
						'value' => $value,
					);
				}
			}
		}

		$raw = array(
			'form_id'   => (string) $form_id,
			'form_name' => (string) $form_title,
			'fields'    => $fields,
		);

		AdapterRegistry::store( $this->normalize( $raw ) );
	}

	/**
	 * Pure transformation: raw Ninja Forms fields -> normalized Lead.
	 *
	 * @param array $raw Keys: form_id, form_name, fields.
	 * @return Lead
	 */
	public function normalize( array $raw ) {
		$lead            = new Lead();
		$lead->source    = $this->slug();
		$lead->form_id   = isset( $raw['form_id'] ) ? (string) $raw['form_id'] : '';
		$lead->form_name = isset( $raw['form_name'] ) ? (string) $raw['form_name'] : '';

		$fields = isset( $raw['fields'] ) && is_array( $raw['fields'] ) ? $raw['fields'] : array();

		foreach ( $fields as $f ) {
			if ( ! is_array( $f ) ) {
				continue;
			}
			$key   = isset( $f['key'] ) ? (string) $f['key'] : '';
			$label = isset( $f['label'] ) && '' !== $f['label'] ? $f['label'] : $key;
			$value = isset( $f['value'] ) ? $f['value'] : '';
			$type  = isset( $f['type'] ) ? $f['type'] : 'text';

			$lead->add_field( $key, $value, $label, $type );

			// Capture uploaded files from Ninja Forms File Uploads (stored permanently)
			if ( 'file_upload' === $type && ! empty( $value ) ) {
				$urls = is_array( $value ) ? $value : explode( ',', $value );
				foreach ( $urls as $url ) {
					$url = trim( $url );
					if ( $url ) {
						$lead->add_attachment( $key, basename( $url ), $url, false );
					}
				}
			}
		}

		return $lead;
	}
}