<?php

namespace SugarCalendarBookings\Migration;

use SugarCalendarBookings\Admin\Area;
use SugarCalendarBookings\Migration\Sources\Amelia\AmeliaAdapter;
use SugarCalendarBookings\Options;
use SugarCalendarBookings\Support\Admin\Notices;
use SugarCalendarBookings\Support\Bootable;

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

/**
 * Amelia Migration Notice.
 *
 * Displays a dismissible admin notice on SCB pages when:
 * - Amelia database tables are detected
 * - Migration has not been performed yet
 *
 * @since 1.3.0
 */
class AmeliaMigrationNotice implements Bootable {

	/**
	 * Notice slug for dismissal tracking.
	 *
	 * @since 1.3.0
	 *
	 * @var string
	 */
	const NOTICE_SLUG = 'amelia_migration_available';

	/**
	 * Notices instance.
	 *
	 * @since 1.3.0
	 *
	 * @var Notices
	 */
	private $notices;

	/**
	 * Options instance.
	 *
	 * @since 1.3.0
	 *
	 * @var Options
	 */
	private $options;

	/**
	 * Admin Area instance.
	 *
	 * @since 1.3.0
	 *
	 * @var Area
	 */
	private $area;

	/**
	 * Constructor.
	 *
	 * @since 1.3.0
	 *
	 * @param Notices $notices Notices instance.
	 * @param Options $options Options instance.
	 * @param Area    $area    Admin Area instance.
	 */
	public function __construct( Notices $notices, Options $options, Area $area ) {

		$this->notices = $notices;
		$this->options = $options;
		$this->area    = $area;
	}

	/**
	 * Whether to load this module.
	 *
	 * @since 1.3.0
	 *
	 * @return bool
	 */
	public function allow_load(): bool {

		return $this->area->is_admin_page();
	}

	/**
	 * Register hooks.
	 *
	 * @since 1.3.0
	 */
	public function hooks() {

		add_action( 'admin_init', [ $this, 'maybe_display_notice' ] );
	}

	/**
	 * Display notice if conditions are met.
	 *
	 * @since 1.3.0
	 */
	public function maybe_display_notice() {

		// Don't show on Import/Export page - user is already there.
		if ( $this->is_import_export_page() ) {
			return;
		}

		// Check if migration has already been performed.
		if ( $this->has_migrated() ) {
			return;
		}

		// Check if Amelia database tables exist.
		if ( ! $this->has_amelia_database() ) {
			return;
		}

		$this->display_notice();
	}

	/**
	 * Check if current page is the Import/Export settings page.
	 *
	 * @since 1.3.0
	 *
	 * @return bool
	 */
	private function is_import_export_page(): bool {

		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$view = isset( $_GET['view'] ) ? sanitize_text_field( wp_unslash( $_GET['view'] ) ) : '';

		return $page === Area::SLUG . '-settings' && $view === 'import-export';
	}

	/**
	 * Check if Amelia migration has been performed.
	 *
	 * @since 1.3.0
	 *
	 * @return bool
	 */
	private function has_migrated(): bool {

		$last_migrated = $this->options->get( 'import.amelia_last_migrated' );

		return ! empty( $last_migrated );
	}

	/**
	 * Check if Amelia database tables exist.
	 *
	 * @since 1.3.0
	 *
	 * @return bool
	 */
	private function has_amelia_database(): bool {

		static $result = null;

		if ( $result === null ) {
			$adapter = new AmeliaAdapter();
			$result  = $adapter->validate() === true;
		}

		return $result;
	}

	/**
	 * Display the migration notice.
	 *
	 * @since 1.3.0
	 */
	private function display_notice() {

		$settings_url = add_query_arg(
			[
				'page' => Area::SLUG . '-settings',
				'view' => 'import-export',
			],
			admin_url( 'admin.php' )
		);

		$message = sprintf(
			wp_kses( /* translators: %1$s - URL to Import/Export settings page. */
				__(
					'We detected Amelia booking data on your site. You can import your services, customers, appointments, and more into Sugar Calendar Bookings. <a href="%1$s">Start Migration</a>',
					'sugar-calendar-bookings-scheduling-appointments-lite'
				),
				[
					'a' => [
						'href' => [],
					],
				]
			),
			esc_url( $settings_url )
		);

		$this->notices->info(
			$message,
			[
				'dismiss' => Notices::DISMISS_USER,
				'slug'    => self::NOTICE_SLUG,
			]
		);
	}
}
