<?php

namespace WPDesk\FakturyKSEF\Api\Services;

use WPDesk\FakturyKSEF\Api\LowLevelAPIClient;
use WPDesk\FakturyKSEF\Api\Requests\GetKSeFPublicKeyRequest;

class CertificateService {


	public const PEM_TRANSIENT_NAME               = 'fi-ksef-pem';
	public const PUBLIC_KEY_SYMMETRIC_ENCRYPTION  = 'SymmetricKeyEncryption';
	public const PUBLIC_KEY_KSEF_TOKEN_ENCRYPTION = 'KsefTokenEncryption';

	private LowLevelAPIClient $http_client;

	public function __construct( LowLevelAPIClient $http_client ) {
		$this->http_client = $http_client;
	}

	public function get_certificates(): array {
		$certificates = get_transient( self::PEM_TRANSIENT_NAME );

		if ( $certificates === false ) {
			$certificates = $this->http_client->execute( new GetKSeFPublicKeyRequest() );
			set_transient( self::PEM_TRANSIENT_NAME, $certificates, DAY_IN_SECONDS );
		}

		return $certificates;
	}

	public function clear_certificates() {
		delete_transient( self::PEM_TRANSIENT_NAME );
	}
}
