<?php
/**
 * Built-in form templates.
 *
 * @package SubmissionInbox
 */

namespace SUBIN\Forms;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Provides default templates for built-in InboxMend forms.
 */
class FormTemplateRepository {
	/**
	 * Form repository.
	 *
	 * @var FormRepository
	 */
	private $forms;

	/**
	 * Field repository.
	 *
	 * @var FormFieldRepository
	 */
	private $fields;

	/**
	 * Constructor.
	 *
	 * @param FormRepository|null      $forms Form repository.
	 * @param FormFieldRepository|null $fields Field repository.
	 */
	public function __construct( FormRepository $forms = null, FormFieldRepository $fields = null ) {
		$this->forms  = $forms ? $forms : new FormRepository();
		$this->fields = $fields ? $fields : new FormFieldRepository();
	}

	/**
	 * Get all templates.
	 *
	 * @return array<string,array<string,mixed>>
	 */
	public function get_templates() {
		return array(
			'contact_us'      => array(
				'title'              => __( 'Contact Us', 'submission-inbox' ),
				'category'           => __( 'General inquiries', 'submission-inbox' ),
				'header_title'       => __( 'Contact us', 'submission-inbox' ),
				'header_description' => __( 'Send us a message and we will get back to you.', 'submission-inbox' ),
				'footer_note'        => __( 'We usually respond within one business day.', 'submission-inbox' ),
				'success_message'    => __( 'Thanks, your message has been sent.', 'submission-inbox' ),
				'submit_button_text' => __( 'Send message', 'submission-inbox' ),
				'fields'             => array(
					$this->field( 'name', 'text', __( 'Name', 'submission-inbox' ), true ),
					$this->field( 'email', 'email', __( 'Email', 'submission-inbox' ), true ),
					$this->field( 'message', 'textarea', __( 'Message', 'submission-inbox' ), true ),
					$this->field( 'consent', 'consent', __( 'I agree to be contacted about this message.', 'submission-inbox' ), false ),
				),
			),
			'request_quote'   => array(
				'title'              => __( 'Request a Quote', 'submission-inbox' ),
				'category'           => __( 'Service requests', 'submission-inbox' ),
				'header_title'       => __( 'Request a quote', 'submission-inbox' ),
				'header_description' => __( 'Tell us what you need and we will review your request.', 'submission-inbox' ),
				'footer_note'        => __( 'Share as much detail as you can so we can respond accurately.', 'submission-inbox' ),
				'success_message'    => __( 'Thanks, your quote request has been sent.', 'submission-inbox' ),
				'submit_button_text' => __( 'Send request', 'submission-inbox' ),
				'fields'             => array(
					$this->field( 'name', 'text', __( 'Name', 'submission-inbox' ), true ),
					$this->field( 'email', 'email', __( 'Email', 'submission-inbox' ), true ),
					$this->field( 'phone', 'phone', __( 'Phone', 'submission-inbox' ), false ),
					$this->field(
						'service_needed',
						'select',
						__( 'Service needed', 'submission-inbox' ),
						false,
						array(
							array(
								'label' => __( 'Consultation', 'submission-inbox' ),
								'value' => 'consultation',
							),
							array(
								'label' => __( 'Project work', 'submission-inbox' ),
								'value' => 'project_work',
							),
							array(
								'label' => __( 'Ongoing support', 'submission-inbox' ),
								'value' => 'ongoing_support',
							),
						)
					),
					$this->field(
						'budget',
						'select',
						__( 'Budget', 'submission-inbox' ),
						false,
						array(
							array(
								'label' => __( 'Not sure yet', 'submission-inbox' ),
								'value' => 'not_sure_yet',
							),
							array(
								'label' => __( 'Small project', 'submission-inbox' ),
								'value' => 'small_project',
							),
							array(
								'label' => __( 'Larger project', 'submission-inbox' ),
								'value' => 'larger_project',
							),
						)
					),
					$this->field( 'message', 'textarea', __( 'Message', 'submission-inbox' ), true ),
					$this->field( 'consent', 'consent', __( 'I agree to be contacted about this request.', 'submission-inbox' ), false ),
				),
			),
			'callback_request' => array(
				'title'              => __( 'Callback Request', 'submission-inbox' ),
				'category'           => __( 'Phone follow-up', 'submission-inbox' ),
				'header_title'       => __( 'Request a callback', 'submission-inbox' ),
				'header_description' => __( 'Leave your details and we will contact you.', 'submission-inbox' ),
				'success_message'    => __( 'Thanks, your callback request has been sent.', 'submission-inbox' ),
				'submit_button_text' => __( 'Request callback', 'submission-inbox' ),
				'fields'             => array(
					$this->field( 'name', 'text', __( 'Name', 'submission-inbox' ), true ),
					$this->field( 'phone', 'phone', __( 'Phone', 'submission-inbox' ), true ),
					$this->field( 'preferred_time', 'text', __( 'Preferred time', 'submission-inbox' ), false ),
					$this->field( 'message', 'textarea', __( 'Message', 'submission-inbox' ), false ),
					$this->field( 'consent', 'consent', __( 'I agree to be contacted about this callback request.', 'submission-inbox' ), false ),
				),
			),
			'support_request' => array(
				'title'              => __( 'Support Request', 'submission-inbox' ),
				'category'           => __( 'Support intake', 'submission-inbox' ),
				'header_title'       => __( 'Support request', 'submission-inbox' ),
				'header_description' => __( 'Describe the issue and we will help you.', 'submission-inbox' ),
				'success_message'    => __( 'Thanks, your support request has been sent.', 'submission-inbox' ),
				'submit_button_text' => __( 'Send support request', 'submission-inbox' ),
				'fields'             => array(
					$this->field( 'name', 'text', __( 'Name', 'submission-inbox' ), true ),
					$this->field( 'email', 'email', __( 'Email', 'submission-inbox' ), true ),
					$this->field( 'subject', 'text', __( 'Subject', 'submission-inbox' ), true ),
					$this->field( 'message', 'textarea', __( 'Message', 'submission-inbox' ), true ),
					$this->field( 'consent', 'consent', __( 'I agree to be contacted about this support request.', 'submission-inbox' ), false ),
				),
			),
			'booking_inquiry' => array(
				'title'              => __( 'Booking Inquiry', 'submission-inbox' ),
				'category'           => __( 'Booking requests', 'submission-inbox' ),
				'header_title'       => __( 'Booking inquiry', 'submission-inbox' ),
				'header_description' => __( 'Tell us what you would like to book.', 'submission-inbox' ),
				'success_message'    => __( 'Thanks, your booking inquiry has been sent.', 'submission-inbox' ),
				'submit_button_text' => __( 'Send inquiry', 'submission-inbox' ),
				'fields'             => array(
					$this->field( 'name', 'text', __( 'Name', 'submission-inbox' ), true ),
					$this->field( 'email', 'email', __( 'Email', 'submission-inbox' ), true ),
					$this->field( 'phone', 'phone', __( 'Phone', 'submission-inbox' ), false ),
					$this->field( 'preferred_date', 'date', __( 'Preferred date', 'submission-inbox' ), false ),
					$this->field( 'message', 'textarea', __( 'Message', 'submission-inbox' ), true ),
					$this->field( 'consent', 'consent', __( 'I agree to be contacted about this booking inquiry.', 'submission-inbox' ), false ),
				),
			),
			'job_application' => array(
				'title'              => __( 'Job Application', 'submission-inbox' ),
				'category'           => __( 'Hiring inquiries', 'submission-inbox' ),
				'header_title'       => __( 'Job application', 'submission-inbox' ),
				'header_description' => __( 'Send us your details and tell us about yourself.', 'submission-inbox' ),
				'footer_note'        => __( 'File uploads are not supported in this version.', 'submission-inbox' ),
				'success_message'    => __( 'Thanks, your application has been sent.', 'submission-inbox' ),
				'submit_button_text' => __( 'Send application', 'submission-inbox' ),
				'fields'             => array(
					$this->field( 'name', 'text', __( 'Name', 'submission-inbox' ), true ),
					$this->field( 'email', 'email', __( 'Email', 'submission-inbox' ), true ),
					$this->field( 'phone', 'phone', __( 'Phone', 'submission-inbox' ), false ),
					$this->field( 'position', 'text', __( 'Position', 'submission-inbox' ), true ),
					$this->field( 'message', 'textarea', __( 'Message', 'submission-inbox' ), true ),
					$this->field( 'consent', 'consent', __( 'I agree to be contacted about this application.', 'submission-inbox' ), false ),
				),
			),
		);
	}

	/**
	 * Get one template.
	 *
	 * @param string $template_key Template key.
	 * @return array<string,mixed>|null
	 */
	public function get_template( $template_key ) {
		$template_key = sanitize_key( $template_key );
		$templates    = $this->get_templates();

		return isset( $templates[ $template_key ] ) ? $templates[ $template_key ] : null;
	}

	/**
	 * Create a built-in form from a template.
	 *
	 * @param string $template_key Template key.
	 * @return int
	 */
	public function create_form_from_template( $template_key ) {
		$template_key = sanitize_key( $template_key );
		$template     = $this->get_template( $template_key );

		if ( ! $template ) {
			return 0;
		}

		$form_id = $this->forms->create(
			array(
				'title'              => $template['title'],
				'template_key'       => $template_key,
				'description'        => $template['header_description'] ?? '',
				'header_title'       => $template['header_title'] ?? '',
				'header_description' => $template['header_description'] ?? '',
				'footer_note'        => $template['footer_note'] ?? '',
				'success_message'    => $template['success_message'] ?? '',
				'submit_button_text' => $template['submit_button_text'] ?? __( 'Send', 'submission-inbox' ),
			)
		);

		if ( $form_id > 0 && ! $this->fields->replace_for_form( $form_id, $template['fields'] ?? array() ) ) {
			$this->forms->delete( $form_id );
			return 0;
		}

		return $form_id;
	}

	/**
	 * Build a field definition.
	 *
	 * @param string             $key Field key.
	 * @param string             $type Field type.
	 * @param string             $label Field label.
	 * @param bool               $required Whether required.
	 * @param array<int,array>   $options Selectable options.
	 * @return array<string,mixed>
	 */
	private function field( $key, $type, $label, $required, array $options = array() ) {
		return array(
			'field_key'     => sanitize_key( $key ),
			'field_type'    => sanitize_key( $type ),
			'field_label'   => sanitize_text_field( $label ),
			'is_required'   => $required ? 1 : 0,
			'field_options' => $options,
		);
	}
}
