<?php

namespace SugarCalendarBookings\Integrations\Zoom;

use SugarCalendarBookings\Integrations\OAuthRelay\AbstractIntegrationActionsOAuthClient;
use SugarCalendarBookings\Integrations\OAuthRelay\OAuthConnectionInterface;
use WP_Error;

/**
 * Zoom Actions Client.
 *
 * Client for Zoom meeting integration actions via Product API.
 *
 * @since 1.4.0
 */
class ZoomActionsClient extends AbstractIntegrationActionsOAuthClient {

	/**
	 * Create a Zoom meeting.
	 *
	 * @since 1.4.0
	 *
	 * @param OAuthConnectionInterface $connection   OAuth connection.
	 * @param array                    $meeting_data Meeting data (topic, start_time, duration, etc.).
	 *
	 * @return array|WP_Error Created meeting data or error.
	 */
	public function create_meeting(
		OAuthConnectionInterface $connection,
		array $meeting_data
	) {

		$response = $this->post( '/zoom/meetings', $connection )
			->json( [ 'params' => [ 'body' => $meeting_data ] ] )
			->send();

		return $this->handle_response( $response );
	}

	/**
	 * Update a Zoom meeting.
	 *
	 * @since 1.4.0
	 *
	 * @param OAuthConnectionInterface $connection   OAuth connection.
	 * @param string                   $meeting_id   Meeting ID to update.
	 * @param array                    $meeting_data Updated meeting data.
	 *
	 * @return array|WP_Error Updated meeting data or error.
	 */
	public function update_meeting(
		OAuthConnectionInterface $connection,
		string $meeting_id,
		array $meeting_data
	) {

		$response = $this->patch( '/zoom/meetings/' . rawurlencode( $meeting_id ), $connection )
			->json( [ 'params' => [ 'body' => $meeting_data ] ] )
			->send();

		return $this->handle_response( $response );
	}

	/**
	 * Delete a Zoom meeting.
	 *
	 * @since 1.4.0
	 *
	 * @param OAuthConnectionInterface $connection OAuth connection.
	 * @param string                   $meeting_id Meeting ID to delete.
	 *
	 * @return true|WP_Error True on success or error.
	 */
	public function delete_meeting(
		OAuthConnectionInterface $connection,
		string $meeting_id
	) {

		$response = $this->delete( '/zoom/meetings/' . rawurlencode( $meeting_id ), $connection )
			->send();

		$result = $this->handle_response( $response );

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

		return true;
	}

	/**
	 * Get the authenticated Zoom user.
	 *
	 * @since 1.4.0
	 *
	 * @param OAuthConnectionInterface $connection OAuth connection.
	 *
	 * @return array|WP_Error User data or error.
	 */
	public function get_user( OAuthConnectionInterface $connection ) {

		$response = $this->get( '/zoom/user', $connection )
			->send();

		return $this->handle_response( $response );
	}

	/**
	 * List scheduled meetings for the authenticated user.
	 *
	 * @since 1.4.0
	 *
	 * @param OAuthConnectionInterface $connection OAuth connection.
	 * @param array                    $params     Query parameters (type, from, to, page_size, next_page_token).
	 *
	 * @return array|WP_Error Meetings list data or error.
	 */
	public function list_meetings( OAuthConnectionInterface $connection, array $params = [] ) {

		$request = $this->get( '/zoom/meetings', $connection );

		if ( ! empty( $params ) ) {
			$request->query( $params );
		}

		return $this->handle_response( $request->send() );
	}
}
