<?php

namespace WPDesk\FakturyKSEF\Api\Repository;

use WPDesk\FakturyKSEF\Api\Model\DocumentState;

class DocumentStateRepository {
	private const STATE_META_KEY         = '_ksef_document_state';
	private const STATUS_META_KEY        = '_ksef_status';
	private const NETWORK_ERROR_META_KEY = '_ksef_network_error';

	public function get( int $post_id ): DocumentState {
		$data = get_post_meta( $post_id, self::STATE_META_KEY, true );

		return DocumentState::from_array( is_array( $data ) ? $data : null );
	}

	public function save( int $post_id, DocumentState $result ): void {
		update_post_meta( $post_id, self::STATE_META_KEY, $result->to_array() );
		update_post_meta( $post_id, self::STATUS_META_KEY, $result->get_status() );
		update_post_meta( $post_id, self::NETWORK_ERROR_META_KEY, $result->is_network_error() ? 'yes' : 'no' );
	}
}
