<?php

/**
 * Vvveb
 *
 * Copyright (C) 2022  Ziadin Givan
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

namespace Vvveb\System\Cart;

/**
 * Provides tax calculation helpers for the cart.
 *
 * @property array<string, array{price: float|int|string, tax_type_id: int|string}> $taxes
 * @property array<int|string, array<array-key, mixed>> $products
 * @property float|int $total_tax
 * @property Tax $tax
 */
trait TaxTrait {
	/**
	 * @param string $name
	 * @param float|int|string $price
	 * @param int|string $taxTypeId
	 */
	public function addTax(string $name, float|int|string $price, int|string $taxTypeId): void {
		if ($price && $taxTypeId) {
			$this->taxes[$name] = ['price' => $price, 'tax_type_id' => $taxTypeId];
		}
	}

	/**
	 * @param string $name
	 */
	public function removeTax(string $name): void {
		unset($this->taxes[$name]);
	}

	/**
	 * Calculate taxes for all products and custom taxes.
	 *
	 * @return array<int, array{tax_rate_id: int|string, name: string, rate: string, type: string, amount: float|int, value: float|int}>
	 */
	public function getTaxes(): array {
		$taxes           = [];
		$this->total_tax = 0;
		$products        = $this->products + $this->taxes;

		foreach ($products as $product) {
			if (isset($product['tax_type_id']) && $product['tax_type_id']) {
				$tax_rates = $this->tax->getRates($product['price'], $product['tax_type_id']);

				foreach ($tax_rates as $tax_rate) {
					$rateId = $tax_rate['tax_rate_id'] ?? false;

					if (! isset($taxes[$rateId])) {
						$taxes[$rateId]          = $tax_rate;
						$taxes[$rateId]['value'] = 0;
					}

					$taxes[$rateId]['value'] += ($tax_rate['amount'] * ($product['quantity'] ?? 1));
					$this->total_tax += ($tax_rate['amount'] * ($product['quantity'] ?? 1));
				}
			}
		}

		return $taxes;
	}

	/**
	 * Get the total tax amount.
	 */
	public function getTaxTotal(): float|int {
		return $this->total_tax;
	}

	/**
	 * Calculate and add all tax totals to the cart totals.
	 */
	public function addTaxTotal(): void {
		$taxes = $this->getTaxes();
		$this->removeTotal('tax');

		foreach ($taxes as $tax) {
			$this->addTotal('tax', 'tax.' . $tax['tax_rate_id'], $tax['name'], $tax['value']);
		}
	}
}
