<?php
/**
 * Form configuration storage and defaults.
 *
 * Stores form configs in a single option. Each form has an ID, title,
 * array of configurable fields, email settings, and a success message.
 *
 * @package ThemeREX\AutoDealer
 */

declare(strict_types=1);

namespace ThemeREX\AutoDealer\Forms;

class FormConfig {

	/**
	 * Option key for all form configurations.
	 */
	public const OPTION_KEY = 'trx_ad_forms';

	/**
	 * Allowed field types.
	 *
	 * @var string[]
	 */
	public const FIELD_TYPES = array(
		'text',
		'email',
		'tel',
		'textarea',
		'date',
		'select',
		'checkbox',
	);

	/**
	 * Get all form configs.
	 *
	 * @return array<string, array> Form ID => config.
	 */
	public static function getAll(): array {
		$forms = get_option( self::OPTION_KEY, array() );

		if ( ! is_array( $forms ) || empty( $forms ) ) {
			return self::getDefaults();
		}

		return $forms;
	}

	/**
	 * Get a single form config by ID.
	 *
	 * @param string $formId Form ID.
	 * @return array|null Form config or null.
	 */
	public static function get( string $formId ): ?array {
		$forms = self::getAll();

		return $forms[ $formId ] ?? null;
	}

	/**
	 * Save all form configs.
	 *
	 * @param array<string, array> $forms Form configs.
	 */
	public static function saveAll( array $forms ): void {
		update_option( self::OPTION_KEY, $forms );
	}

	/**
	 * Get form options for select dropdowns (ID => title).
	 *
	 * @return array<string, string>
	 */
	public static function getOptions(): array {
		$forms   = self::getAll();
		$options = array();

		foreach ( $forms as $id => $form ) {
			$options[ $id ] = $form['title'] ?? $id;
		}

		return $options;
	}

	/**
	 * Seed default forms if none exist.
	 *
	 * @return void
	 */
	public static function seedDefaults(): void {
		$existing = get_option( self::OPTION_KEY, array() );

		if ( ! empty( $existing ) ) {
			return;
		}

		update_option( self::OPTION_KEY, self::getDefaults() );
	}

	/**
	 * Default form configurations.
	 *
	 * @return array<string, array>
	 */
	public static function getDefaults(): array {
		return array(
			'inquiry'    => array(
				'title'             => __( 'Vehicle Inquiry', 'trx-auto-dealer' ),
				'fields'            => array(
					array(
						'id'          => 'name',
						'label'       => __( 'Your Name', 'trx-auto-dealer' ),
						'type'        => 'text',
						'required'    => true,
						'placeholder' => __( 'John Doe', 'trx-auto-dealer' ),
						'width'       => '50',
					),
					array(
						'id'          => 'email',
						'label'       => __( 'Email Address', 'trx-auto-dealer' ),
						'type'        => 'email',
						'required'    => true,
						'placeholder' => __( 'john@example.com', 'trx-auto-dealer' ),
						'width'       => '50',
					),
					array(
						'id'          => 'phone',
						'label'       => __( 'Phone Number', 'trx-auto-dealer' ),
						'type'        => 'tel',
						'required'    => false,
						'placeholder' => __( '+1 (555) 000-0000', 'trx-auto-dealer' ),
						'width'       => '100',
					),
					array(
						'id'          => 'message',
						'label'       => __( 'Message', 'trx-auto-dealer' ),
						'type'        => 'textarea',
						'required'    => false,
						'placeholder' => __( 'I am interested in this vehicle...', 'trx-auto-dealer' ),
						'width'       => '100',
					),
				),
				'email_to'          => '',
				'email_subject'     => __( 'New Inquiry: {vehicle_title}', 'trx-auto-dealer' ),
				'email_body'        => "Name: {customer_name}\nEmail: {field:email}\nPhone: {field:phone}\n\nVehicle: {vehicle_title}\nURL: {vehicle_url}\n\nMessage:\n{field:message}",
				'success_message'   => __( 'Thank you for your inquiry! We will get back to you shortly.', 'trx-auto-dealer' ),
				'submit_label'      => __( 'Send Inquiry', 'trx-auto-dealer' ),
				'autoreply'         => true,
				'autoreply_subject' => __( 'We received your inquiry about {vehicle_title}', 'trx-auto-dealer' ),
				'autoreply_body'    => "Hi {customer_name},\n\nThanks for reaching out about {vehicle_title}. We've forwarded your inquiry to {dealer_name} and someone will follow up shortly.\n\nVehicle: {vehicle_title}\nURL: {vehicle_url}\n\n— {site_name}",
			),
			'test_drive' => array(
				'title'             => __( 'Test Drive Booking', 'trx-auto-dealer' ),
				'fields'            => array(
					array(
						'id'          => 'name',
						'label'       => __( 'Your Name', 'trx-auto-dealer' ),
						'type'        => 'text',
						'required'    => true,
						'placeholder' => __( 'John Doe', 'trx-auto-dealer' ),
						'width'       => '50',
					),
					array(
						'id'          => 'email',
						'label'       => __( 'Email Address', 'trx-auto-dealer' ),
						'type'        => 'email',
						'required'    => true,
						'placeholder' => __( 'john@example.com', 'trx-auto-dealer' ),
						'width'       => '50',
					),
					array(
						'id'          => 'phone',
						'label'       => __( 'Phone Number', 'trx-auto-dealer' ),
						'type'        => 'tel',
						'required'    => true,
						'placeholder' => __( '+1 (555) 000-0000', 'trx-auto-dealer' ),
						'width'       => '50',
					),
					array(
						'id'          => 'date',
						'label'       => __( 'Preferred Date', 'trx-auto-dealer' ),
						'type'        => 'date',
						'required'    => true,
						'placeholder' => '',
						'width'       => '50',
					),
					array(
						'id'          => 'message',
						'label'       => __( 'Additional Notes', 'trx-auto-dealer' ),
						'type'        => 'textarea',
						'required'    => false,
						'placeholder' => __( 'Any special requests...', 'trx-auto-dealer' ),
						'width'       => '100',
					),
				),
				'email_to'          => '',
				'email_subject'     => __( 'Test Drive Request: {vehicle_title}', 'trx-auto-dealer' ),
				'email_body'        => "Name: {customer_name}\nEmail: {field:email}\nPhone: {field:phone}\nPreferred Date: {field:date}\n\nVehicle: {vehicle_title}\nURL: {vehicle_url}\n\nNotes:\n{field:message}",
				'success_message'   => __( 'Thank you! Your test drive request has been submitted. We will confirm your appointment shortly.', 'trx-auto-dealer' ),
				'submit_label'      => __( 'Book Test Drive', 'trx-auto-dealer' ),
				'autoreply'         => true,
				'autoreply_subject' => __( 'Test drive request received — {vehicle_title}', 'trx-auto-dealer' ),
				'autoreply_body'    => "Hi {customer_name},\n\nWe've received your test drive request for {vehicle_title} on {field:date}. {dealer_name} will confirm your appointment shortly.\n\nVehicle: {vehicle_title}\nURL: {vehicle_url}\n\n— {site_name}",
			),
		);
	}

	/**
	 * Get available merge tags with descriptions.
	 *
	 * @return array<string, string> Tag => description.
	 */
	public static function getMergeTags(): array {
		return array(
			'{vehicle_title}'  => __( 'Vehicle title (Year Make Model)', 'trx-auto-dealer' ),
			'{vehicle_url}'    => __( 'Vehicle page URL', 'trx-auto-dealer' ),
			'{vehicle_price}'  => __( 'Vehicle price', 'trx-auto-dealer' ),
			'{vehicle_stock}'  => __( 'Stock number', 'trx-auto-dealer' ),
			'{dealer_name}'    => __( 'Dealer name', 'trx-auto-dealer' ),
			'{dealer_phone}'   => __( 'Dealer phone', 'trx-auto-dealer' ),
			'{dealer_email}'   => __( 'Dealer email', 'trx-auto-dealer' ),
			'{site_name}'      => __( 'Site name', 'trx-auto-dealer' ),
			'{customer_name}'  => __( 'Submitter name (alias for {field:name})', 'trx-auto-dealer' ),
			'{customer_email}' => __( 'Submitter email (alias for {field:email})', 'trx-auto-dealer' ),
			'{form_type}'      => __( 'Form ID (e.g. inquiry, test_drive)', 'trx-auto-dealer' ),
			'{form_title}'     => __( 'Form display title', 'trx-auto-dealer' ),
			'{field:*}'        => __( 'Any form field value (e.g. {field:name}, {field:phone})', 'trx-auto-dealer' ),
		);
	}
}
