<?php

declare (strict_types=1);
namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\Response;

use WC_Order_Item_Product;
use WC_Order;
/**
 * Data source from WC_Order (completed checkout).
 *
 * @see https://ucp.dev/2026-01-23/schemas/shopping/checkout.json
 */
final class OrderDataSource implements CheckoutDataSource, ProvidesOrderConfirmation, ProvidesPaymentInstruments
{
    private WC_Order $order;
    private CouponTitleFormatter $coupon_formatter;
    public function __construct(WC_Order $order, CouponTitleFormatter $coupon_formatter)
    {
        $this->order = $order;
        $this->coupon_formatter = $coupon_formatter;
    }
    public function get_status(): string
    {
        return 'completed';
    }
    public function get_currency(): string
    {
        return $this->order->get_currency();
    }
    public function get_line_items(): array
    {
        $line_items = [];
        $index = 1;
        foreach ($this->order->get_items() as $item) {
            /** @var WC_Order_Item_Product $item */
            $product = $item->get_product();
            $subtotal_cents = (int) \round((float) $item->get_subtotal() * 100);
            $total_cents = (int) \round((float) $item->get_total() * 100);
            $discount_cents = $subtotal_cents - $total_cents;
            $line_items[] = ['id' => 'li_' . $index, 'item' => ['id' => (string) $product->get_id(), 'title' => $item->get_name(), 'price' => $product ? (int) \round((float) $product->get_price() * 100) : 0], 'quantity' => $item->get_quantity(), 'totals' => [['type' => 'subtotal', 'amount' => $subtotal_cents], ['type' => 'discount', 'amount' => $discount_cents], ['type' => 'total', 'amount' => $total_cents]]];
            ++$index;
        }
        return $line_items;
    }
    public function get_totals(): array
    {
        $subtotal_cents = (int) \round((float) $this->order->get_subtotal() * 100);
        $discount_cents = (int) \round((float) $this->order->get_discount_total() * 100);
        $shipping_cents = (int) \round((float) $this->order->get_shipping_total() * 100);
        $tax_cents = (int) \round((float) $this->order->get_total_tax() * 100);
        $total_cents = (int) \round((float) $this->order->get_total() * 100);
        return [['type' => 'subtotal', 'amount' => $subtotal_cents], ['type' => 'discount', 'amount' => $discount_cents], ['type' => 'fulfillment', 'amount' => $shipping_cents], ['type' => 'tax', 'amount' => $tax_cents], ['type' => 'total', 'amount' => $total_cents]];
    }
    public function get_buyer(): array
    {
        $buyer = ['email' => $this->order->get_billing_email() ?: '', 'first_name' => $this->order->get_billing_first_name() ?: '', 'last_name' => $this->order->get_billing_last_name() ?: '', 'phone_number' => $this->order->get_billing_phone() ?: ''];
        $consent = $this->order->get_meta('_ucp_buyer_consent');
        if (\is_array($consent)) {
            $buyer['consent'] = $consent;
        }
        return $buyer;
    }
    public function get_fulfillment(): array
    {
        $destinations = [];
        if ($this->order->get_shipping_address_1()) {
            $destinations[] = ['id' => 'dest_1', 'address_country' => $this->order->get_shipping_country(), 'postal_code' => $this->order->get_shipping_postcode(), 'address_region' => $this->order->get_shipping_state(), 'address_locality' => $this->order->get_shipping_city(), 'street_address' => $this->order->get_shipping_address_1()];
        }
        $method = ['id' => 'method_1', 'type' => 'shipping', 'line_item_ids' => $this->get_line_item_ids()];
        if (!empty($destinations)) {
            $method['destinations'] = $destinations;
            $method['selected_destination_id'] = 'dest_1';
        }
        return ['methods' => [$method]];
    }
    public function get_discounts(): array
    {
        $codes = [];
        $applied = [];
        foreach ($this->order->get_coupon_codes() as $wc_code) {
            $codes[] = $wc_code;
        }
        foreach ($this->order->get_coupons() as $coupon_item) {
            $discount_cents = (int) \round((float) $coupon_item->get_discount() * 100);
            if ($discount_cents > 0) {
                $applied[] = ['code' => $coupon_item->get_code(), 'title' => $this->coupon_formatter->from_order_item($coupon_item), 'amount' => $discount_cents, 'allocations' => []];
            }
        }
        return ['codes' => $codes, 'applied' => $applied];
    }
    public function get_order(): array
    {
        return ['id' => (string) $this->order->get_id(), 'number' => $this->order->get_order_number(), 'permalink_url' => $this->order->get_checkout_order_received_url()];
    }
    public function get_payment(): array
    {
        $payment = $this->order->get_meta('_ucp_payment');
        if (!\is_array($payment) || empty($payment['instruments'])) {
            return ['instruments' => []];
        }
        $instruments = [];
        foreach ($payment['instruments'] as $instrument) {
            $instrument['selected'] = \true;
            $instruments[] = $instrument;
        }
        return ['instruments' => $instruments];
    }
    /**
     * @return array<string>
     */
    private function get_line_item_ids(): array
    {
        $line_item_ids = [];
        $index = 1;
        foreach ($this->order->get_items() as $item) {
            $line_item_ids[] = 'li_' . $index;
            ++$index;
        }
        return $line_item_ids;
    }
}
