<?php

namespace SugarCalendarBookings\Admin;

use SugarCalendarBookings\Support\Admin\Notices;
use SugarCalendarBookings\Support\Bootable;

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

/**
 * Global query notices.
 *
 * @since 1.0.0
 */
class GlobalQueryNotices implements Bootable {

	/**
	 * Notices.
	 *
	 * @since 1.0.0
	 * @since 1.2.0 Made protected.
	 *
	 * @var Notices
	 */
	protected $notices;

	/**
	 * Constructor.
	 *
	 * @since 1.0.0
	 *
	 * @param Notices $notices Notices.
	 */
	public function __construct( $notices ) {

		$this->notices = $notices;
	}

	/**
	 * Whenever to load module.
	 *
	 * @since 1.0.0
	 *
	 * @return bool
	 */
	public function allow_load() {

		return ! empty( $this->notices->get_query_notice() );
	}

	/**
	 * Hooks.
	 *
	 * @since 1.0.0
	 */
	public function hooks() {

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

	/**
	 * Handle global query notices.
	 *
	 * @since 1.0.0
	 */
	public function handle_notices() { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.MaxExceeded

		switch ( $this->notices->get_query_notice() ) {
			case 'service_created':
				$this->notices->success( esc_html__( 'Service created successfully.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
				break;

			case 'service_updated':
				$this->notices->success( esc_html__( 'Service updated successfully.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
				break;

			case 'service_deleted':
				$this->notices->success( esc_html__( 'Service deleted successfully.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
				break;

			case 'appointment_created':
				$this->notices->success( esc_html__( 'Appointment created successfully.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
				break;

			case 'appointment_updated':
				$this->notices->success( esc_html__( 'Appointment updated successfully.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
				break;

			case 'appointment_cancelled':
				$this->notices->success( esc_html__( 'Appointment cancelled successfully.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
				break;

			case 'appointment_deleted':
				$this->notices->success( esc_html__( 'Appointment deleted successfully.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
				break;

			case 'notification_pending_resent':
				$this->notices->success( esc_html__( 'Pending notification has been resent successfully.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
				break;

			case 'notification_confirmation_resent':
				$this->notices->success( esc_html__( 'Confirmation notification has been resent successfully.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
				break;

			case 'notification_update_resent':
				$this->notices->success( esc_html__( 'Update notification has been resent successfully.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
				break;

			case 'notification_cancellation_resent':
				$this->notices->success( esc_html__( 'Cancellation notification has been resent successfully.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
				break;

			case 'notification_reminder_resent':
				$this->notices->success( esc_html__( 'Reminder notification has been resent successfully.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
				break;

			case 'notification_refund_resent':
				$this->notices->success( esc_html__( 'Refund notification has been resent successfully.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
				break;

			case 'customer_created':
				$this->notices->success( esc_html__( 'Customer created successfully.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
				break;

			case 'customer_updated':
				$this->notices->success( esc_html__( 'Customer updated successfully.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
				break;

			case 'customer_deleted':
				$this->notices->success( esc_html__( 'Customer deleted successfully.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
				break;

			case 'location_created':
				$this->notices->success( esc_html__( 'Location created successfully.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
				break;

			case 'location_updated':
				$this->notices->success( esc_html__( 'Location updated successfully.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
				break;

			case 'location_deleted':
				$this->notices->success( esc_html__( 'Location deleted successfully.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
				break;

			case 'date_override_created':
				$this->notices->success( esc_html__( 'Date override created successfully.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
				break;

			case 'date_override_updated':
				$this->notices->success( esc_html__( 'Date override updated successfully.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
				break;

			case 'date_override_deleted':
				$this->notices->success( esc_html__( 'Date override deleted successfully.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
				break;

			case 'oauth_connected':
				// phpcs:ignore WordPress.Security.NonceVerification.Recommended
				$provider      = isset( $_GET['integration'] ) ? sanitize_text_field( wp_unslash( $_GET['integration'] ) ) : '';
				$provider_name = ucfirst( $provider );

				$this->notices->success(
					sprintf(
					/* translators: %1$s - provider name (e.g., Google, Zoom). */
						esc_html__( '%1$s account connected successfully.', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
						esc_html( $provider_name )
					)
				);
				break;

			case 'oauth_error':
				$data          = $this->notices->get_query_notice_data();
				$error_message = $data[0] ?? esc_html__( 'Authorization failed. Please try again.', 'sugar-calendar-bookings-scheduling-appointments-lite' );

				$this->notices->error( esc_html( $error_message ) );
				break;

			case 'registration_failed':
				$data     = $this->notices->get_query_notice_data();
				$provider = ! empty( $data[0] ) ? sanitize_key( $data[0] ) : '';

				// Error message passed as a separate query param to avoid comma-splitting issues.
				$error_message = isset( $_GET['scbookings-notice-error'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
					? sanitize_text_field( wp_unslash( $_GET['scbookings-notice-error'] ) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
					: '';

				$notice_text = ! empty( $error_message )
					? sprintf(
						/* translators: %1$s - error message from the registration attempt. */
						esc_html__( 'Unable to connect to Sugar Calendar services: %1$s', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
						esc_html( $error_message )
					)
					: esc_html__( 'Unable to connect to Sugar Calendar services. Please try again.', 'sugar-calendar-bookings-scheduling-appointments-lite' );

				if ( ! empty( $provider ) ) {
					$retry_url = add_query_arg(
						[
							'action'   => 'scb_integration_connect',
							'provider' => $provider,
							'_wpnonce' => wp_create_nonce( 'scb_integration_connect' ),
						],
						admin_url( 'admin.php' )
					);

					$notice_text .= ' ' . sprintf(
						wp_kses( /* translators: %1$s - retry URL. */
							__( '<a href="%1$s">Click here to try again</a>.', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
							[
								'a' => [
									'href' => [],
								],
							]
						),
						esc_url( $retry_url )
					);
				}

				$this->notices->error( $notice_text );
				break;
		}
	}
}
