<?php

namespace SugarCalendarBookings\Migration\Sources\Amelia;

use SugarCalendarBookings\Migration\MigrationUIConfigInterface;
use SugarCalendarBookings\Support\Helpers\Helpers;

/**
 * Amelia UI Config.
 *
 * Provides UI configuration for the Amelia migration modal.
 * Separates UI/presentation concerns from data migration concerns.
 *
 * @since 1.3.0
 */
class AmeliaUIConfig implements MigrationUIConfigInterface {

	/**
	 * Amelia adapter instance.
	 *
	 * @since 1.3.0
	 *
	 * @var AmeliaAdapter
	 */
	private AmeliaAdapter $adapter;

	/**
	 * Constructor.
	 *
	 * @since 1.3.0
	 *
	 * @param AmeliaAdapter $adapter Amelia adapter instance.
	 */
	public function __construct( AmeliaAdapter $adapter ) {

		$this->adapter = $adapter;
	}

	/**
	 * Get the source adapter ID.
	 *
	 * @since 1.3.0
	 *
	 * @return string
	 */
	public function get_source_id(): string {

		return $this->adapter->get_id();
	}

	/**
	 * Get the display name for this migration source.
	 *
	 * @since 1.3.0
	 *
	 * @return string
	 */
	public function get_source_name(): string {

		return $this->adapter->get_name();
	}

	/**
	 * Check if Amelia migration is available.
	 *
	 * @since 1.3.0
	 *
	 * @return bool
	 */
	public function is_available(): bool {

		return ! is_wp_error( $this->adapter->validate() );
	}

	/**
	 * Get modal configuration for Amelia migration.
	 *
	 * @since 1.3.0
	 *
	 * @return array<string, mixed>
	 */
	public function get_modal_config(): array {

		return [
			'header'        => [
				'source_icon'     => SCBOOKINGS_PLUGIN_URL . 'assets/images/migration/amelia-icon.png',
				'source_icon_alt' => __( 'Amelia', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
				'title'           => __( 'Import data from Amelia with one click!', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
			],
			'options'       => $this->get_modal_options(),
			'info_message'  => $this->get_pro_info_message(),
			'entity_labels' => [
				'service'     => __( 'Services', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
				'location'    => __( 'Locations', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
				'customer'    => __( 'Customers', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
				'employee'    => __( 'Employees', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
				'appointment' => __( 'Appointments', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
			],
			'i18n'          => [
				'confirm_button'               => __( 'One Click Import', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
				'cancel_button'                => __( 'Cancel', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
				'complete_title'               => __( 'Migration Complete!', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
				'complete_description_success' => __( 'You successfully migrated from Amelia to Sugar Calendar Bookings.', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
				'complete_description_partial' => $this->get_partial_complete_description(),
			],
			'support_url'   => Helpers::get_support_url(
				[
					'wpf96206_20' => 'Sugar Calendar Bookings',
					'wpf96206_5'  => 'Bug report or problem',
					'wpf96206_6'  => site_url(),
				]
			),
		];
	}

	/**
	 * Get modal options configuration.
	 *
	 * @since 1.3.0
	 *
	 * @return array<array<string, mixed>>
	 */
	private function get_modal_options(): array {

		return [
			[
				'value'       => 'default',
				'label'       => __( 'Default Import', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
				'description' => __( 'Import all services, locations, customers and appointments', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
				'is_default'  => true,
			],
			[
				'value'            => 'advanced',
				'label'            => __( 'Advanced Import', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
				'description'      => __( 'Advanced control on what to import', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
				'advanced_options' => $this->get_advanced_options(),
			],
		];
	}

	/**
	 * Get advanced import options configuration.
	 *
	 * @since 1.3.0
	 *
	 * @return array<array<string, mixed>>
	 */
	private function get_advanced_options(): array {

		$options = [
			[
				'name'        => 'import_services',
				'label'       => __( 'Import all Services', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
				'entity_type' => 'service',
				'checked'     => true,
				'disabled'    => false,
				'requires'    => [ 'location' ],
			],
			[
				'name'        => 'import_locations',
				'label'       => __( 'Import all Locations', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
				'entity_type' => 'location',
				'checked'     => true,
				'disabled'    => false,
			],
			[
				'name'        => 'import_customers',
				'label'       => __( 'Import all Customers', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
				'entity_type' => 'customer',
				'checked'     => true,
				'disabled'    => false,
			],
		];

		if ( sugar_calendar_bookings()->is_pro() ) {
			$options[] = [
				'name'        => 'import_employees',
				'label'       => __( 'Import all Employees', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
				'entity_type' => 'employee',
				'checked'     => true,
				'disabled'    => false,
			];
		}

		$options[] = [
			'name'        => 'import_appointments',
			'label'       => __( 'Import all Appointments', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
			'entity_type' => 'appointment',
			'checked'     => true,
			'disabled'    => false,
			'requires'    => [ 'service', 'customer', 'location' ],
		];

		return $options;
	}

	/**
	 * Get the Pro info message if applicable.
	 *
	 * @since 1.3.0
	 *
	 * @return string
	 */
	private function get_pro_info_message(): string {

		// Only show if Amelia Pro is detected but Sugar Calendar Bookings is Lite.
		if ( ! $this->adapter->is_pro() || sugar_calendar_bookings()->is_pro() ) {
			return '';
		}

		$upgrade_url = Helpers::get_utm_url(
			'https://sugarcalendar.com/pricing/bookings/',
			[
				'medium'  => 'amelia-migration-modal',
				'content' => 'amelia-pro-notice',
			]
		);

		return sprintf(
			/* translators: %1$s - opening bold tag; %2$s - closing bold tag; %3$s - opening link tag; %4$s - closing link tag. */
			__( '%1$sComing from Amelia Pro?%2$s Some data and features from Amelia Pro requires Sugar Calendar Bookings Pro and won\'t be imported now. You can %3$supgrade to Pro%4$s and re-migrate later.', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
			'<b>',
			'</b>',
			'<a href="' . esc_url( $upgrade_url ) . '" target="_blank" rel="noopener noreferrer">',
			'</a>'
		);
	}

	/**
	 * Get the partial completion description with documentation link.
	 *
	 * Shown when some items were skipped or failed during migration.
	 *
	 * @since 1.3.0
	 *
	 * @return string
	 */
	private function get_partial_complete_description(): string {

		return sprintf(
			/* translators: %1$s - opening link tag; %2$s - closing link tag. */
			__( 'You\'ve migrated from Amelia to Sugar Calendar Bookings, but some items may not have been imported. Check our %1$sdocumentation%2$s for assistance.', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
			'<a href="' . esc_url( Helpers::get_utm_url( 'https://sugarcalendar.com/docs/bookings/migrating-from-amelia/', [ 'content' => 'amelia-migration' ] ) ) . '" target="_blank" rel="noopener noreferrer">',
			'</a>'
		);
	}
}
