<?php

namespace WPDesk\FakturyKSEF\Services;

use WPDesk\FakturyKSEF\Repository\PluginSettings;

class InitialDataValidation {

	public const ALLOWED_DOCUMENT_TYPES = [ 'invoice', 'correction' ];
	public const ALLOWED_POST_STATUSES  = [ 'publish' ];

	private PluginSettings $settings;

	public function __construct( PluginSettings $settings ) {
		$this->settings = $settings;
	}

	public function validate( $document ): array {
		$reasons = [];

		$customer      = $document->get_customer();
		$country       = $customer->get_country() ?? false;
		$document_type = $document->get_type();

		if ( ! $this->settings->is_cross_border_sending_enabled() ) {
			if ( $country !== 'PL' ) {
				// translators: 1. country
				$reasons[] = sprintf( esc_html__( 'Country (%s) isn\'t allowed', 'ksef-for-flexible-invoices' ), $country );
			}
		}

		if ( ! in_array( $document_type, self::ALLOWED_DOCUMENT_TYPES, true ) ) {
			// translators: 1. document type
			$reasons[] = sprintf( esc_html__( 'Document type (%s) isn\'t allowed', 'ksef-for-flexible-invoices' ), $document_type );
		}

		if ( ! in_array( get_post_status( $document->get_id() ), self::ALLOWED_POST_STATUSES, true ) ) {
			$reasons[] = esc_html__( 'Unpublished document cannot be sent', 'ksef-for-flexible-invoices' );
		}

		return apply_filters( 'fiksef_services_initial_validation_reasons', $reasons, $document );
	}
}
