<?php

declare( strict_types=1 );

namespace Brightfern\Invoice\Model;

/**
 * One row of the VAT breakdown (BG-23). There is exactly one TaxSubtotal per
 * unique (category code, rate) pair across the whole invoice — this is the
 * crux of "mixed VAT" handling and the source of the BR-CO-* reconciliation
 * rules in schematron.
 *
 *   BT-116  Taxable amount for this (category, rate).
 *   BT-117  VAT amount for this (category, rate).
 *   BT-118  VAT category code.
 *   BT-119  VAT rate (percent).
 *   BT-120  Exemption reason text   (required for E/AE/K/G/O).
 *   BT-121  Exemption reason code   (VATEX-*).
 */
final class TaxSubtotal {
	public string $categoryCode;
	public float $percent;
	public float $taxableAmount = 0.0;
	public float $taxAmount = 0.0;
	public ?string $exemptionReasonText = null;
	public ?string $exemptionReasonCode = null;

	public function __construct( string $categoryCode, float $percent ) {
		$this->categoryCode = $categoryCode;
		$this->percent      = $percent;
	}

	/** Stable key used to group lines/allowances into the breakdown. */
	public static function key( string $categoryCode, float $percent ): string {
		return $categoryCode . '|' . number_format( $percent, 2, '.', '' );
	}

	public function keyForSelf(): string {
		return self::key( $this->categoryCode, $this->percent );
	}
}
