<?php

declare( strict_types=1 );

namespace Brightfern\Invoice\Model;

/**
 * A postal address (BG-5 seller / BG-8 buyer).
 *
 * BT-40/BT-50  line one          (street)
 * BT-41/BT-51  line two          (additional)
 * BT-37/BT-52  city
 * BT-38/BT-53  post code
 * BT-39/BT-54  country subentity (region/state)
 * BT-40/BT-55  country code (ISO 3166-1 alpha-2) — MANDATORY in EN 16931.
 */
final class Address {
	public string $line1 = '';
	public string $line2 = '';
	public string $city = '';
	public string $postalZone = '';
	public string $countrySubentity = '';
	/** ISO 3166-1 alpha-2, upper-case. The single mandatory address field. */
	public string $countryCode = '';

	public function isInEu(): bool {
		return in_array( strtoupper( $this->countryCode ), self::EU_COUNTRIES, true );
	}

	/**
	 * EU member states, as ISO 3166-1 alpha-2 country codes — for intra-community
	 * vs export classification (shipping/billing COUNTRY, not a VAT number).
	 *
	 * Do NOT reuse this to interpret a VAT number's two-letter prefix: Greece is
	 * the one EU state where the two diverge under Article 215 of the VAT
	 * Directive — its ISO code is GR, but VIES and every Greek VAT number use
	 * EL. A VAT number's prefix belongs against {@see EU_VAT_PREFIXES} instead.
	 */
	public const EU_COUNTRIES = array(
		'AT', 'BE', 'BG', 'HR', 'CY', 'CZ', 'DK', 'EE', 'FI', 'FR', 'DE', 'GR',
		'HU', 'IE', 'IT', 'LV', 'LT', 'LU', 'MT', 'NL', 'PL', 'PT', 'RO', 'SK',
		'SI', 'ES', 'SE',
	);

	/**
	 * The two-letter prefixes VIES and other VAT-number schemes actually use —
	 * a DIFFERENT list from {@see EU_COUNTRIES}. Two divergences from plain
	 * ISO 3166-1: Greece is EL (not its ISO code GR), and Northern Ireland uses
	 * XI for goods VAT under the post-Brexit protocol (no ISO code of its own;
	 * it is not a country). Canonical source for both PartyValidator's VIES
	 * lookups and Fa3Serializer's TKodyKrajowUE (KSeF's own enum matches this
	 * list exactly) — keep it in exactly one place so the two cannot drift
	 * apart the way EU_COUNTRIES and a second hand-copied list once did.
	 */
	public const EU_VAT_PREFIXES = array(
		'AT', 'BE', 'BG', 'CY', 'CZ', 'DK', 'EE', 'FI', 'FR', 'DE', 'EL', 'HR',
		'HU', 'IE', 'IT', 'LV', 'LT', 'LU', 'MT', 'NL', 'PL', 'PT', 'RO', 'SK',
		'SI', 'ES', 'SE', 'XI',
	);
}
