<?php

namespace POPxPMPro\Providers;

use WcElectronInvoice\Providers\OrderItemAbstract;

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

class OrderItem extends OrderItemAbstract {

    /**
     * @var \MemberOrder
     */
    protected $order_item;

    private function get_membership_level() {
        if ( ! is_object( $this->order_item ) || ! method_exists( $this->order_item, 'getMembershipLevel' ) ) {
            return null;
        }

        $level = $this->order_item->getMembershipLevel();

        return is_object( $level ) ? $level : null;
    }

    function get_id() {
        $level = $this->get_membership_level();

        return ( is_object( $level ) && isset( $level->id ) ) ? (int) $level->id : 0;
    }

    function get_product() {
        return new Product( $this->get_membership_level() );
    }

    function get_name() {
        $level = $this->get_membership_level();

        return ( is_object( $level ) && isset( $level->name ) ) ? $level->name : '';
    }

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

    function get_total_tax() {
        return floatval( $this->order_item->tax );
    }

    function get_sku() {
        // PMPro doesn't support SKUs
        return $this->order_item->membership_id;
    }

    function get_data() {
        return [
            'id'           => $this->get_id(),
            'order_id'     => $this->order_item->id,
            'name'         => $this->get_name(),
            'product_id'   => $this->get_product_id(),
            'variation_id' => $this->get_product_id(),
            'quantity'     => 1,
            'tax_class'    => $this->get_tax_class(),
            'subtotal'     => $this->get_total(),
            'subtotal_tax' => $this->get_total_tax(),
            'total'        => $this->get_total(),
            'total_tax'    => $this->get_total_tax(),
            'taxes'        => [
                'subtotal' => [ 1 => $this->get_total_tax() ],
                'total'    => [ 1 => $this->get_total_tax() ],
            ],
            'meta_data'    => [],
            'provider'     => 'pmpro',
        ];
    }

    function get_tax_class() {
        return '';
    }

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