<?php

namespace EDD\Pro\Invoices\XML\Elements;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore

class TaxTotal extends Element {

	/**
	 * Get the data.
	 *
	 * @since 1.4.0
	 * @return array
	 */
	protected function get_data(): array {
		$tax_rate    = $this->get_effective_tax_rate();
		$tax_amount  = $this->order->tax;
		$category_id = $this->get_tax_category();

		// Taxable amount = line subtotals - discount + fee subtotals (BR-CO-14).
		$fee_subtotal = 0.0;
		foreach ( $this->order->get_fees() as $fee ) {
			$fee_subtotal += (float) $fee->subtotal;
		}
		$taxable = (float) $this->order->subtotal - (float) $this->order->discount + $fee_subtotal;

		// Unlike invoice lines and allowances/charges (BR-O-05/06/07) which must
		// OMIT cbc:Percent for category "O", the VAT breakdown (BG-23) requires
		// cbc:Percent to be present and equal to 0 (BR-O-10).
		$tax_category_children = array(
			array(
				'name'  => 'cbc:ID',
				'value' => $category_id,
			),
			array(
				'name'  => 'cbc:Percent',
				'value' => $this->format_number( $tax_rate ),
			),
		);

		// BR-AE-10, BR-E-10, BR-O-10: exemption reason required. BR-Z-10: must NOT have reason.
		$exemption_reason = $this->get_tax_exemption_reason( $category_id );
		if ( ! empty( $exemption_reason ) ) {
			$tax_category_children[] = array(
				'name'  => 'cbc:TaxExemptionReason',
				'value' => $exemption_reason,
			);
		}

		$tax_category_children[] = array(
			'name'     => 'cac:TaxScheme',
			'children' => array(
				array(
					'name'  => 'cbc:ID',
					'value' => 'VAT',
				),
			),
		);

		return array(
			'name'     => 'cac:TaxTotal',
			'children' => array(
				array(
					'name'       => 'cbc:TaxAmount',
					'value'      => $this->format_number( $tax_amount ),
					'attributes' => array(
						'currencyID' => $this->order->currency,
					),
				),
				// EDD currently applies a single tax rate per order. If multi-rate support is added,
				// this will need one cac:TaxSubtotal per distinct rate/category (Peppol BIS 3.0).
				array(
					'name'     => 'cac:TaxSubtotal',
					'children' => array(
						array(
							'name'       => 'cbc:TaxableAmount',
							'value'      => $this->format_number( $taxable ),
							'attributes' => array(
								'currencyID' => $this->order->currency,
							),
						),
						array(
							'name'       => 'cbc:TaxAmount',
							'value'      => $this->format_number( $tax_amount ),
							'attributes' => array(
								'currencyID' => $this->order->currency,
							),
						),
						array(
							'name'     => 'cac:TaxCategory',
							'children' => $tax_category_children,
						),
					),
				),
			),
		);
	}

	/**
	 * Get the tax exemption reason for a given category.
	 *
	 * @since 3.6.7
	 * @param string $category_id The tax category code.
	 * @return string Exemption reason text, or empty if not applicable.
	 */
	private function get_tax_exemption_reason( string $category_id ): string {
		$reasons = array(
			'AE' => 'Reverse charge',
			'E'  => 'Exempt from tax',
			'O'  => 'Not subject to VAT',
		);

		return $reasons[ $category_id ] ?? '';
	}
}
