<?php
/**
 * Scheduler calendar connection controls.
 *
 * @package Niwa_AI
 */

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

class Niwa_AI_Calendar_Integration_UI {

	/** Render calendar result notices at the top of Scheduler. */
	public static function render_notices() {
		if ( ! empty( $_GET['calendar_error'] ) ) {
			echo '<div class="notice notice-error inline"><p>' . esc_html( sanitize_text_field( wp_unslash( $_GET['calendar_error'] ) ) ) . '</p></div>';
		} elseif ( ! empty( $_GET['calendar_connected'] ) ) {
			echo '<div class="notice notice-success inline"><p>' . esc_html__( 'Google Calendar connected successfully.', 'niwa-ai' ) . '</p></div>';
		} elseif ( ! empty( $_GET['calendar_invite_sent'] ) ) {
			echo '<div class="notice notice-success inline"><p>' . esc_html__( 'Calendar connection link sent to the team member.', 'niwa-ai' ) . '</p></div>';
		} elseif ( ! empty( $_GET['calendar_disconnected'] ) ) {
			echo '<div class="notice notice-success inline"><p>' . esc_html__( 'Calendar disconnected.', 'niwa-ai' ) . '</p></div>';
		}
	}

	/** Render the administrator connection next to the main Scheduler controls. */
	public static function render_admin() {
		$service    = new Niwa_AI_Calendar_Integration_Service();
		$connection = $service->get_connection( 'admin' );

		echo '<div class="niwa-card niwa-calendar-admin-card">';
		echo '<div class="niwa-card__header niwa-scheduler-intro"><div class="niwa-card__icon"><span class="dashicons dashicons-calendar-alt" aria-hidden="true"></span></div><div><h2 class="niwa-card__title">' . esc_html__( 'Administrator calendar', 'niwa-ai' ) . '</h2><p class="niwa-field__hint">' . esc_html__( 'Keep every Niwa booking visible in the administrator calendar.', 'niwa-ai' ) . '</p></div></div>';
		echo '<div class="niwa-card__body"><div class="niwa-calendar-provider-grid">';
		self::render_google_control( $service, 'admin', $connection, true );
		self::render_apple_unavailable();
		echo '</div></div></div>';
	}

	/** Render connection state and invite action directly below a member email. */
	public static function render_member( array $member ) {
		$member_id = sanitize_key( (string) ( $member['id'] ?? '' ) );
		$email     = sanitize_email( (string) ( $member['email'] ?? '' ) );
		echo '<div class="niwa-calendar-member-controls">';
		if ( '' === $member_id || '' === $email ) {
			echo '<span class="niwa-field__hint">' . esc_html__( 'Save this member and email address before sending a calendar connection.', 'niwa-ai' ) . '</span></div>';
			return;
		}

		$service    = new Niwa_AI_Calendar_Integration_Service();
		$owner      = 'member:' . $member_id;
		$connection = $service->get_connection( $owner );
		if ( ! empty( $connection ) ) {
			$account = ! empty( $connection['account_email'] ) ? (string) $connection['account_email'] : __( 'Google Calendar connected', 'niwa-ai' );
			echo '<span class="niwa-calendar-connection-status"><span class="dashicons dashicons-yes-alt" aria-hidden="true"></span>' . esc_html( $account ) . '</span>';
			if ( ! empty( $connection['needs_reconnect'] ) ) {
				echo '<span class="niwa-field__hint">' . esc_html__( 'Send a new link so this member can approve busy-time access.', 'niwa-ai' ) . '</span>';
			}
			echo self::disconnect_link( $owner, __( 'Disconnect', 'niwa-ai' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- helper escapes URL and label.
		}

		$send_url = wp_nonce_url(
			add_query_arg(
				array( 'action' => 'niwa_ai_calendar_send_invite', 'member_id' => $member_id ),
				admin_url( 'admin-post.php' )
			),
			'niwa_ai_calendar_send_invite'
		);
		echo '<a class="button niwa-btn niwa-btn--secondary" href="' . esc_url( $send_url ) . '"><span class="dashicons dashicons-email-alt" aria-hidden="true"></span>' . esc_html__( 'Send calendar connection link', 'niwa-ai' ) . '</a>';
		echo '<span class="niwa-field__hint">' . esc_html__( 'The member receives a private page with Connect with Google. Apple will appear only when real live access is available.', 'niwa-ai' ) . '</span>';
		echo '</div>';
	}

	private static function render_google_control( Niwa_AI_Calendar_Integration_Service $service, $owner, array $connection, $admin ) {
		echo '<div class="niwa-calendar-provider"><div><strong>Google Calendar</strong>';
		if ( ! empty( $connection ) ) {
			$account = ! empty( $connection['account_email'] ) ? (string) $connection['account_email'] : __( 'Connected', 'niwa-ai' );
			echo '<span class="niwa-calendar-connection-status"><span class="dashicons dashicons-yes-alt" aria-hidden="true"></span>' . esc_html( $account ) . '</span>';
		}
		echo '</div><div class="niwa-calendar-provider__actions">';

		if ( $admin ) {
			$url = wp_nonce_url(
				add_query_arg( 'action', 'niwa_ai_calendar_google_start', admin_url( 'admin-post.php' ) ),
				'niwa_ai_calendar_google_start'
			);
			$label = ! empty( $connection ) ? __( 'Reconnect with Google', 'niwa-ai' ) : __( 'Connect with Google', 'niwa-ai' );
			if ( $service->is_google_available() ) {
				echo '<a class="button niwa-btn niwa-btn--primary" href="' . esc_url( $url ) . '">' . esc_html( $label ) . '</a>';
			} else {
				echo '<button type="button" class="button niwa-btn niwa-btn--secondary" disabled>' . esc_html( $label ) . '</button>';
			}
		}
		if ( ! empty( $connection ) ) {
			echo self::disconnect_link( $owner, __( 'Disconnect', 'niwa-ai' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- helper escapes URL and label.
		}
		echo '</div>';
		if ( ! $service->is_google_available() ) {
			echo '<span class="niwa-field__hint">' . esc_html__( 'This server does not provide the secure encryption required for Google Calendar connection.', 'niwa-ai' ) . '</span>';
		} else {
			echo '<span class="niwa-field__hint">' . esc_html__( 'One Google approval enables live booking sync. Team-member calendars also block externally busy times.', 'niwa-ai' ) . '</span>';
		}
		echo '</div>';
	}

	private static function render_apple_unavailable() {
		echo '<div class="niwa-calendar-provider is-unavailable"><div><strong>Apple Calendar</strong></div><div class="niwa-calendar-provider__actions"><button type="button" class="button niwa-btn niwa-btn--secondary" disabled>' . esc_html__( 'Connect with Apple', 'niwa-ai' ) . '</button></div><span class="niwa-field__hint">' . esc_html__( 'Live Apple Calendar authorization is not enabled. No calendar file or read-only subscription will be offered as a substitute.', 'niwa-ai' ) . '</span></div>';
	}

	private static function disconnect_link( $owner, $label ) {
		$url = wp_nonce_url(
			add_query_arg(
				array( 'action' => 'niwa_ai_calendar_disconnect', 'owner' => $owner ),
				admin_url( 'admin-post.php' )
			),
			'niwa_ai_calendar_disconnect'
		);
		return '<a class="button niwa-btn niwa-btn--danger niwa-calendar-disconnect" href="' . esc_url( $url ) . '">' . esc_html( $label ) . '</a>';
	}
}
