<?php

namespace WooPoPCozmosLabsPMS\Providers;

use WcElectronInvoice\Providers\OrderItemAbstract;
use WcElectronInvoice\Providers\TaxOrderItemAbstract;

defined( 'ABSPATH' ) || die( 'Not allowed.' );

class TaxOrderItem extends TaxOrderItemAbstract {

	/**
	 * PMS_Payment
	 * @var
	 */
	protected $order_item;


	function get_name() {
		// always return an empty string
		return '';
	}

	function get_total() {
		return $this->order_item->amount;
	}

	function get_total_tax() {

		$taxSettings = TaxesProvider::get_tax_settings();
		if ( empty( $taxSettings ) ) {
			return 0;
		}

		return $this->order_item->total_tax;
	}

	function get_data() {

		$data = [
			'id'           => $this->get_id(),
			'order_id'     => $this->order_item->id,
			'name'         => $this->get_name(),
			'product_id'   => $this->get_product_id(),
			'rate_code'    => $this->get_rate_code(),
			'rate_id'      => $this->get_rate_id(),
			'label'        => $this->get_label(),
			'rate_percent' => $this->get_rate_percent(),
			'tax_total'    => $this->get_total(),
			'compound'     => '',
			'meta_data'    => array(),
			'provider'     => 'cozmoslabspms',
		];

		return $data;
	}

	public function get_rate_code() {

		$taxSettings        = TaxesProvider::get_tax_settings();
		$merchantVatCountry = isset( $taxSettings['merchant-vat-country'] ) ? $taxSettings['merchant-vat-country'] : '';
		if ( empty( $merchantVatCountry ) ) {
			return '';
		}

		return sprintf( '%1$s-%2$s-%3$s', $merchantVatCountry, 'IMPOSTE', $this->get_id() );
	}

	public function get_rate_id() {
		return 1;
	}

	public function get_label() {
		return esc_html__( 'Imposte', WC_EL_INV_TEXTDOMAIN );
	}

	/**
	 * @inheritDoc
	 */
	function get_rate_percent() {
		return pms_get_payment_meta( $this->order_item->id, 'pms_tax_rate', true );
	}

	function get_product_id() {
		return $this->order_item->subscription_id;
	}

	function get_id() {
		// PMS doesn't have a tax id
		// so we return 1
		return 1;
	}
}
