<?php
/**
 * Model for BNPL transaction.
 */

namespace Flo_Checkout\Gateways\BNPL\Models;

/**
 * Class BNPL_Transaction
 */
class BNPL_Transaction {
	/**
	 * The unique identifier for the object.
	 * Example id: 3fa85f64-5717-4562-b3fc-2c963f66afa6
	 *
	 * @var string
	 */
	protected string $transaction_id;

	/**
	 * The unique identifier for a payment.
	 * Example id: 3fa85f64-5717-4562-b3fc-2c963f66afa6
	 *
	 * @var string
	 */
	protected string $payment_id;

	/**
	 * The phone number associated with a user.
	 *
	 * @var string
	 */
	protected string $phone_number;

	/**
	 * The total amount including tax.
	 *
	 * @var float
	 */
	protected float $amount_with_tax;

	/**
	 * Transaction note.
	 *
	 * @var ?string
	 */
	protected ?string $note;

	/**
	 * Refund note.
	 *
	 * @var ?string
	 */
	protected ?string $refund_note;

	/**
	 * Status of the transaction.
	 * 0: Not started
	 * 1: Started (opened link)
	 * 2: Completed
	 * 3: Cancelled
	 *
	 * @var int
	 */
	protected int $status;

	/**
	 * Indicates whether a refund is available for the transaction.
	 *
	 * @var bool
	 */
	protected bool $refund_available;

	/**
	 * Date and time when the transaction was created.
	 * Example date: 2024-01-04T19:51:45.131Z
	 *
	 * @var string
	 */
	protected string $created_at;

	/**
	 * Last update date and time.
	 * Example date: 2024-01-04T19:51:45.131Z
	 *
	 * @var ?string
	 */
	protected ?string $last_update;

	/**
	 * QR code image data.
	 *
	 * @var string
	 */
	protected string $qr_code;

	/**
	 * BNPL URL.
	 *
	 * @var string
	 */
	protected string $application_url;

	/**
	 * BNPL_Transaction constructor.
	 *
	 * @param array $data Data.
	 */
	public function __construct( array $data = array() ) {

		$key_map = array(
			'id'              => 'transaction_id',
			'paymentId'       => 'payment_id',
			'phoneNumber'     => 'phone_number',
			'amountWithTax'   => 'amount_with_tax',
			'note'            => 'note',
			'status'          => 'status',
			'refundAvailable' => 'refund_available',
			'createdAt'       => 'created_at',
			'lastUpdate'      => 'last_update',
			'qrCode'          => 'qr_code',
			'qrCodeBytes'     => 'qr_code',
			'applicationUrl'  => 'application_url',
			'refundNote'      => 'refund_note',
		);

		foreach ( $key_map as $data_key => $property_name ) {
			if ( isset( $data[ $data_key ] ) ) {
				$this->$property_name = $data[ $data_key ];
			}
		}
	}

	/**
	 * Returns the ID value.
	 *
	 * @return ?string The ID value.
	 */
	public function get_transaction_id(): ?string {
		return $this->transaction_id ?? null;
	}

	/**
	 * Gets the payment ID.
	 *
	 * @return ?string The payment ID.
	 */
	public function get_payment_id(): ?string {
		return $this->payment_id ?? null;
	}

	/**
	 * Retrieves the phone number associated with this object.
	 *
	 * @return string The phone number.
	 */
	public function get_phone_number(): string {
		return $this->phone_number ?? '';
	}

	/**
	 * Get the amount with tax.
	 *
	 * @return ?float The amount with tax.
	 */
	public function get_amount_with_tax(): ?float {
		return $this->amount_with_tax ?? null;
	}

	/**
	 * Returns the note associated with this object.
	 *
	 * @return string The note.
	 */
	public function get_note(): string {
		return $this->note ?? '';
	}

	/**
	 * Returns the refund note value.
	 *
	 * @return string The refund note value.
	 */
	public function get_refund_note(): string {
		return $this->refund_note ?? '';
	}

	/**
	 * Retrieves the status of the object.
	 *
	 * @return ?int The status value of the object.
	 */
	public function get_status(): ?int {
		return $this->status ?? null;
	}

	/**
	 * Retrieves the status of the object.
	 *
	 * @return string The status value of the object.
	 */
	public function get_status_text(): string {
		return match ( $this->get_status() ) {
			0 => 'not_started',
			1 => 'started', // Opened link.
			2 => 'completed',
			3 => 'cancelled',
			default => 'unknown',
		};
	}

	/**
	 * Check if refund is available.
	 *
	 * @return bool Returns true if refund is available, false otherwise.
	 */
	public function is_refund_available(): bool {
		return $this->refund_available ?? false;
	}

	/**
	 * Retrieves the creation date of an object.
	 *
	 * This method returns the value of the `created_at` property of the current object.
	 *
	 * @return string The creation date in string format.
	 */
	public function get_created_at(): string {
		return $this->created_at ?? '';
	}

	/**
	 * Returns the last update value.
	 *
	 * @return string The last update value.
	 */
	public function get_last_update(): string {
		return $this->last_update ?? '';
	}

	/**
	 * Retrieves the QR code associated with the current object.
	 *
	 * @return string The QR code.
	 */
	public function get_qr_code(): string {
		return $this->qr_code ?? '';
	}

	/**
	 * Retrieves the URL of the application.
	 *
	 * @return string The URL of the application.
	 */
	public function get_application_url(): string {
		return $this->application_url ?? '';
	}
}
