<?php
/**
 * Kit REST Routes
 *
 * @package Sequensy\Integrations\Kit
 * @since 1.0.0
 */

declare(strict_types=1);

namespace Sequensy\Integrations\Kit\Rest;

use Sequensy\Integrations\Kit\KitApi;
use Sequensy\Rest\AbstractIntegrationRoutes;
use Sequensy\Rest\IntegrationRouteDescriptor;
use WP_REST_Request;
use WP_REST_Response;
use WP_Error;

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

/**
 * Class KitRoutes
 *
 * Registers Kit integration REST routes for dynamic selects.
 */
class KitRoutes extends AbstractIntegrationRoutes {

	/**
	 * Configure the integration route descriptor.
	 *
	 * @return IntegrationRouteDescriptor
	 */
	protected function configure(): IntegrationRouteDescriptor {
		return new IntegrationRouteDescriptor(
			slug: 'kit',
			display_name: __( 'Kit', 'sequensy' ),
			credential_fields: array(
				'api_key' => array(
					'required' => true,
					'type'     => 'string',
				),
			),
		);
	}

	/**
	 * Create a Kit API instance with the given credentials.
	 *
	 * @param array $credentials Credential values with 'api_key'.
	 * @return KitApi API client instance.
	 */
	protected function create_api( array $credentials ): object {
		return new KitApi( $credentials['api_key'] );
	}

	/**
	 * Register integration-specific sub-routes.
	 *
	 * @return void
	 */
	protected function register_sub_routes(): void {
		$this->register_connection_get_route( 'tags', array( $this, 'get_tags' ) );
		$this->register_connection_get_route( 'forms', array( $this, 'get_forms' ) );
		$this->register_connection_get_route( 'sequences', array( $this, 'get_sequences' ) );
		$this->register_connection_get_route( 'segments', array( $this, 'get_segments' ) );
		$this->register_connection_get_route( 'email-templates', array( $this, 'get_email_templates' ) );
	}

	/**
	 * Get available tags for a connection.
	 *
	 * @param WP_REST_Request $request Full data about the request.
	 * @return WP_REST_Response|WP_Error Response object or error.
	 */
	public function get_tags( WP_REST_Request $request ): WP_REST_Response|WP_Error {
		$connection_id = $request->get_param( 'connection_id' );

		return $this->api_call_with_connection(
			$connection_id,
			function ( KitApi $api ) {
				$result = $api->list_tags();

				if ( is_wp_error( $result ) ) {
					return $result;
				}

				$tags = $result['data']['tags'] ?? array();

				return array_map(
					function ( array $tag ) {
						return array(
							'id'   => $tag['id'],
							'name' => $tag['name'],
						);
					},
					$tags
				);
			}
		);
	}

	/**
	 * Get available forms for a connection.
	 *
	 * @param WP_REST_Request $request Full data about the request.
	 * @return WP_REST_Response|WP_Error Response object or error.
	 */
	public function get_forms( WP_REST_Request $request ): WP_REST_Response|WP_Error {
		$connection_id = $request->get_param( 'connection_id' );

		return $this->api_call_with_connection(
			$connection_id,
			function ( KitApi $api ) {
				$result = $api->list_forms();

				if ( is_wp_error( $result ) ) {
					return $result;
				}

				$forms = $result['data']['forms'] ?? array();

				return array_map(
					function ( array $form ) {
						return array(
							'id'   => $form['id'],
							'name' => $form['name'],
						);
					},
					$forms
				);
			}
		);
	}

	/**
	 * Get available sequences for a connection.
	 *
	 * @param WP_REST_Request $request Full data about the request.
	 * @return WP_REST_Response|WP_Error Response object or error.
	 */
	public function get_sequences( WP_REST_Request $request ): WP_REST_Response|WP_Error {
		$connection_id = $request->get_param( 'connection_id' );

		return $this->api_call_with_connection(
			$connection_id,
			function ( KitApi $api ) {
				$result = $api->list_sequences();

				if ( is_wp_error( $result ) ) {
					return $result;
				}

				$sequences = $result['data']['sequences'] ?? array();

				return array_map(
					function ( array $sequence ) {
						return array(
							'id'   => $sequence['id'],
							'name' => $sequence['name'],
						);
					},
					$sequences
				);
			}
		);
	}

	/**
	 * Get available segments for a connection.
	 *
	 * @param WP_REST_Request $request Full data about the request.
	 * @return WP_REST_Response|WP_Error Response object or error.
	 */
	public function get_segments( WP_REST_Request $request ): WP_REST_Response|WP_Error {
		$connection_id = $request->get_param( 'connection_id' );

		return $this->api_call_with_connection(
			$connection_id,
			function ( KitApi $api ) {
				$result = $api->list_segments();

				if ( is_wp_error( $result ) ) {
					return $result;
				}

				$segments = $result['data']['segments'] ?? array();

				return array_map(
					function ( array $segment ) {
						return array(
							'id'   => $segment['id'],
							'name' => $segment['name'],
						);
					},
					$segments
				);
			}
		);
	}

	/**
	 * Get available email templates for a connection.
	 *
	 * @param WP_REST_Request $request Full data about the request.
	 * @return WP_REST_Response|WP_Error Response object or error.
	 */
	public function get_email_templates( WP_REST_Request $request ): WP_REST_Response|WP_Error {
		$connection_id = $request->get_param( 'connection_id' );

		return $this->api_call_with_connection(
			$connection_id,
			function ( KitApi $api ) {
				$result = $api->list_email_templates();

				if ( is_wp_error( $result ) ) {
					return $result;
				}

				$templates = $result['data']['email_templates'] ?? array();

				return array_map(
					function ( array $template ) {
						return array(
							'id'   => $template['id'],
							'name' => $template['name'],
						);
					},
					$templates
				);
			}
		);
	}
}
