<?php

declare( strict_types=1 );

namespace Brightfern\Invoice\Input;

/**
 * Normalized, WordPress-free representation of a WooCommerce order, consumed by
 * OrderInvoiceMapper. The WooCommerce adapter builds it; tests build it directly.
 */
final class OrderData {
	public string $number = '';
	public string $issueDate = '';   // YYYY-MM-DD.
	public string $dueDate = '';
	public string $currency = 'EUR';
	public string $buyerReference = '';
	public string $orderReference = '';
	public string $note = '';
	public float $prepaidAmount = 0.0;

	/** True when this represents a credit note (a WooCommerce refund) rather than an invoice. */
	public bool $isCreditNote = false;
	/** BT-25: the invoice number being credited. Required when isCreditNote is true. */
	public string $precedingInvoiceNumber = '';
	/** BT-26: issue date of the preceding invoice (optional). */
	public string $precedingInvoiceIssueDate = '';

	// Buyer identity (BG-7).
	public string $buyerName = '';
	public string $buyerVat = '';
	public string $buyerCompanyId = '';
	public string $buyerCompanyIdScheme = '';
	public string $buyerEndpointId = '';
	public string $buyerEndpointScheme = '';
	public string $buyerEmail = '';
	public string $buyerPhone = '';

	// Buyer address (BG-8).
	public string $buyerLine1 = '';
	public string $buyerLine2 = '';
	public string $buyerCity = '';
	public string $buyerPostalZone = '';
	public string $buyerCountrySubentity = '';
	public string $buyerCountryCode = '';

	/**
	 * Country the goods are taxed/shipped to — drives intra-community vs export
	 * classification when a line's VAT rate is 0. Defaults to the buyer country.
	 */
	public string $shipToCountryCode = '';

	/**
	 * Explicit domestic reverse-charge flag (rare for e-commerce; e.g. certain
	 * B2B services). When true, 0-rated domestic lines map to category AE.
	 */
	public bool $reverseCharge = false;

	/** @var OrderLine[] */
	public array $lines = array();

	/** @var OrderCharge[] Document-level shipping/fees/discounts. */
	public array $charges = array();

	public function shipToCountry(): string {
		return '' !== $this->shipToCountryCode ? $this->shipToCountryCode : $this->buyerCountryCode;
	}
}
