<?php

namespace Licenzo\Modules\WooCommerce;

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

use Licenzo\Model\Product;
use Licenzo\Model\ProductVariation;
use Licenzo\Services\Integrations\ProviderAliasService;

class WooCommerceHelper
{
    public static function getCustomerData($order)
    {
        return [
            'email'      => $order->get_billing_email(),
            'first_name' => $order->get_billing_first_name(),
            'last_name'  => $order->get_billing_last_name(),
            'ip'         => $order->get_customer_ip_address(),
        ];
    }

    public static function getOrderProducts($order)
    {
        $products = [];
        foreach ($order->get_items() as $item_id => $item) {
            $product = $item->get_product();

            if (!$product) {
                continue;
            }

            $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
            $variation_id = $product->is_type('variation') ? $product->get_id() : null;

            $product_data = [
                'id'           => $product_id,
                'variation_id' => $variation_id,
                'name'         => $item->get_name(),
                'price'        => floatval($item->get_subtotal()) / max(1, $item->get_quantity()),
                'quantity'     => $item->get_quantity(),
                'variations'   => [],
            ];

            $db_product = Product::whereIn('provider', ProviderAliasService::wooAliases())
                ->where('provider_id', (string) $product_id)
                ->first();

            if ($db_product) {
                $product_data = array_merge($product_data, [
                    'type' => $db_product->type,
                    'provider' => $db_product->provider,
                    'product_link' => $db_product->product_link,
                ]);
            }

            if ($product->is_type('variation')) {
                $variation_data = [
                    'id'       => $variation_id,
                    'name'     => $item->get_name(),
                    'price'    => floatval($item->get_subtotal()) / max(1, $item->get_quantity()),
                    'quantity' => $item->get_quantity(),
                ];
                $variation = ProductVariation::where('provider_id', (string) $variation_id)->first();

                if ($variation) {
                    $variation_data = array_merge($variation_data, [
                        'billing_cycle' => $variation->billing_cycle,
                        'max_activations' => $variation->max_activations,
                        'trial_days'      => $variation->trial_days,
                        'support_for_unit' => $variation->support_for_unit,
                        'support_for'      => $variation->support_for
                    ]);
                }
                $product_data['variations'][] = $variation_data;
            }
            $products[] = $product_data;
        }
        return $products;
    }
}
