<?php

namespace includes\api;

use includes\http\WTV_Request;

/**
 * Class WTV_Notes
 *
 * Provides functionality for managing notes, generating payloads, and syncing customer order notes to an external API.
 *
 * @since 2.5.3
 * @clickup https://app.clickup.com/t/8694f6j2r
 */
class WTV_Notes {
	/**
	 * Generates a payload array with the provided parameters.
	 *
	 * @param string $attached_to The identifier indicating what the payload is attached to.
	 * @param string $text Optional text to include in the payload. Defaults to an empty string.
	 * @param string $subject Optional subject to include in the payload.
	 * @param int|null $documentType Optional document type identifier to include in the payload.
	 * @param bool|null $is_done Optional flag indicating completion status, if provided.
	 *
	 * @return array An array representing the generated payload with the provided data.
	 */
	public static function generate_payload( string $attached_to, string $text = "", string $subject = "", ?int $document_type = null, ?bool $is_done = null ): array {
		$return = array(
			'AttachedTo' => $attached_to,
			'Text'       => $text
		);
		if ( $subject ) {
			$return['Subject'] = $subject;
		}
		if ( $document_type ) {
			$return['DocumentType'] = $document_type;
		}
		if ( is_bool( $is_done ) ) {
			$return['IsDone'] = $is_done;
		}

		return $return;
	}

	/**
	 * Send the payload to the Notes API endpoint
	 *
	 * @param array $payload The payload to send
	 *
	 * @return array|null The API response or null on failure
	 * @throws \Exception if the request fails
	 */
	public static function create( array $payload ) {
		$endpoint = '/notes';
		//exception caught in callstack above
		$response = WTV_Request::post( $endpoint, $payload );

		return $response;
	}

	/**
	 * Handles syncing customer order notes to Visma after order creation.
	 *
	 * @param \WC_Order $wc_order The WooCommerce order object containing customer details and notes.
	 * @param array|mixed $visma_order The Visma order data returned from the API. Must be an array containing an 'Id' key.
	 *
	 * @return void
	 */
	public static function visma_after_order_sync_handler( $wc_order, $visma_order ) {
		if ( is_array( $visma_order ) && isset( $visma_order['Id'] ) ) {

			$customer_note = $wc_order->get_customer_note();
			if ( $customer_note ) {
				$payload = self::generate_payload( $visma_order['Id'], $customer_note , 'Customer order note' );
				try {
					$response = self::create( $payload );
					do_action( 'visma_after_note_sent', $wc_order->get_customer_note() , $response );
				} catch ( \Throwable $t ) {
					wtv_write_log( __CLASS__ . '::' . __FUNCTION__ . ' failed to send note: ' . $t->getMessage() );
				}
			}
		} else {
			wtv_write_log( __CLASS__ . '::' . __FUNCTION__ . ' skipping sending notes for order: ' . $wc_order->get_id() . ', no visma order id found' );
		}
	}

	/**
	 * Initializes the necessary hooks and actions for the functionality.
	 *
	 * @return void
	 */
	public static function init() {
		add_action( 'visma_after_order_sync', array( __CLASS__, 'visma_after_order_sync_handler' ), 10, 2 );
	}

	/***
	 * Made for testing purposes
	 */
	public static function get_note($visma_note_id){
		//this is not used in production code, only in tests, no need to wrap in try catch
		return WTV_Request::get( "/notes/{$visma_note_id}");
	}
}
