<?php

declare( strict_types=1 );

namespace Brightfern\Invoice\Input;

/**
 * A single normalized order line fed to the mapper. Deliberately decoupled from
 * WC_Order_Item so the mapper is unit-testable with plain fixtures; the
 * WooCommerce adapter (src/WooCommerce/WooOrderAdapter) fills these in.
 *
 * Amounts are NET (excluding VAT), in the document currency.
 *   netBeforeDiscount = WC line subtotal (price × qty, pre-coupon).
 *   netAfterDiscount  = WC line total    (post-coupon). This becomes BT-131.
 * The difference is surfaced as a line-level allowance (BG-27) so the discount
 * is visible and BR-24 (price × qty − allowances = line net) holds exactly.
 */
final class OrderLine {
	public function __construct(
		public string $id,
		public string $name,
		public float $quantity,
		public float $netBeforeDiscount,
		public float $netAfterDiscount,
		public float $taxRatePercent,
		public string $sku = '',
		public string $gtin = '',
		public string $unitCode = 'C62',
		public string $description = '',
	) {}

	public function discount(): float {
		return $this->netBeforeDiscount - $this->netAfterDiscount;
	}

	/** Unit net price BEFORE discount (BT-146 basis). */
	public function unitPrice(): float {
		return $this->quantity > 0.0 ? $this->netBeforeDiscount / $this->quantity : 0.0;
	}
}
