<?php
/**
 * Contact Form 7 source adapter.
 *
 * @package Leadite
 */

namespace Leadite\Capture;

use Leadite\Storage\Lead;

defined( 'ABSPATH' ) || exit;

class Cf7Adapter implements SourceAdapter {

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

	public function label() {
		return 'Contact Form 7';
	}

	public function is_available() {
		return class_exists( 'WPCF7' ) || defined( 'WPCF7_VERSION' );
	}

	public function register() {
		// Fires after CF7 has accepted a valid submission.
		add_action( 'wpcf7_mail_sent', array( $this, 'capture' ) );
	}

	/**
	 * Capture hook: assemble the raw payload, normalize, store.
	 *
	 * @param \WPCF7_ContactForm $contact_form Submitted form.
	 */
	public function capture( $contact_form ) {
		if ( ! class_exists( '\WPCF7_Submission' ) ) {
			return;
		}
		$submission = \WPCF7_Submission::get_instance();
		if ( ! $submission ) {
			return;
		}

		$posted = $submission->get_posted_data();
		if ( empty( $posted ) || ! is_array( $posted ) ) {
			return;
		}

		// Map tag types so canonical guessing is more accurate.
		$types = array();
		if ( method_exists( $contact_form, 'scan_form_tags' ) ) {
			foreach ( $contact_form->scan_form_tags() as $tag ) {
				if ( ! empty( $tag->name ) ) {
					$types[ $tag->name ] = $tag->basetype;
				}
			}
		}

		$raw = array(
			'form_id'        => method_exists( $contact_form, 'id' ) ? (string) $contact_form->id() : '',
			'form_name'      => method_exists( $contact_form, 'title' ) ? (string) $contact_form->title() : '',
			'posted'         => $posted,
			'types'          => $types,
			'uploaded_files' => method_exists( $submission, 'uploaded_files' ) ? $submission->uploaded_files() : array(),
		);

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

	/**
	 * Pure transformation: raw CF7 payload -> normalized Lead.
	 *
	 * @param array $raw Keys: form_id, form_name, posted, types, uploaded_files.
	 * @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'] : '';

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

		foreach ( $posted as $key => $value ) {
			// Skip CF7 internal fields.
			if ( '_' === substr( $key, 0, 1 ) || 'g-recaptcha-response' === $key ) {
				continue;
			}
			$type = isset( $types[ $key ] ) ? $types[ $key ] : 'text';
			$lead->add_field( $key, $value, $key, $type );
		}

		// Attach uploaded files
		$uploaded = isset( $raw['uploaded_files'] ) && is_array( $raw['uploaded_files'] ) ? $raw['uploaded_files'] : array();
		foreach ( $uploaded as $field_key => $paths ) {
			$paths = (array) $paths;
			foreach ( $paths as $path ) {
				if ( $path ) {
					$lead->add_attachment( $field_key, basename( $path ), $path, true );
				}
			}
		}

		return $lead;
	}
}