<?php

declare(strict_types=1);

/*
 * PaypalServerSDKLib
 *
 * This file was automatically generated by APIMATIC v3.0 ( https://www.apimatic.io ).
 */

namespace PaypalServerSDKLib\Tests\E2E;

use PaypalServerSDKLib\Controllers\OrdersController;
use PaypalServerSDKLib\Models\Builders\AddressBuilder;
use PaypalServerSDKLib\Models\Builders\AmountBreakdownBuilder;
use PaypalServerSDKLib\Models\Builders\AmountWithBreakdownBuilder;
use PaypalServerSDKLib\Models\Builders\ItemBuilder;
use PaypalServerSDKLib\Models\Builders\MoneyBuilder;
use PaypalServerSDKLib\Models\Builders\OrderRequestBuilder;
use PaypalServerSDKLib\Models\Builders\PatchBuilder;
use PaypalServerSDKLib\Models\Builders\PaymentSourceBuilder;
use PaypalServerSDKLib\Models\Builders\PayPalWalletBuilder;
use PaypalServerSDKLib\Models\Builders\PayPalWalletExperienceContextBuilder;
use PaypalServerSDKLib\Models\Builders\PurchaseUnitRequestBuilder;
use PaypalServerSDKLib\Models\Builders\ShippingDetailsBuilder;
use PaypalServerSDKLib\Models\CheckoutPaymentIntent;
use PaypalServerSDKLib\Models\Order;
use PaypalServerSDKLib\Models\PatchOp;
use PaypalServerSDKLib\Models\PayPalExperienceLandingPage;
use PaypalServerSDKLib\Tests\Controllers\BaseTestController;

class PatchOrderFlowTest extends BaseTestController
{
    /**
     * @var OrdersController OrdersController instance
     */
    protected static $ordersController;

    /**
     * Setup test class
     */
    public static function setUpBeforeClass(): void
    {
        $client = parent::getClient();
        self::$ordersController = $client->getOrdersController();
    }

    public function testCreateOrder(): Order
    {
        $request = [
            'body' => OrderRequestBuilder::init(
                CheckoutPaymentIntent::CAPTURE,
                [
                    PurchaseUnitRequestBuilder::init(
                        AmountWithBreakdownBuilder::init(
                            'USD',
                            '10.00'
                        )
                            ->breakdown(
                                AmountBreakdownBuilder::init()
                                    ->itemTotal(
                                        MoneyBuilder::init(
                                            'USD',
                                            '10.00'
                                        )->build()
                                    )
                                    ->shipping(
                                        MoneyBuilder::init(
                                            'USD',
                                            '0.00'
                                        )->build()
                                    )
                                    ->taxTotal(
                                        MoneyBuilder::init(
                                            'USD',
                                            '0'
                                        )->build()
                                    )->build()
                            )->build()
                    )
                        ->description('Camera Shop')
                        ->items([
                            ItemBuilder::init(
                                'Levis 501 Selvedge STF',
                                MoneyBuilder::init(
                                    'USD',
                                    '5.00'
                                )->build(),
                                '1'
                            )
                                ->tax(MoneyBuilder::init(
                                    'USD',
                                    '0.00'
                                )->build())
                                ->sku('5158936')
                                ->build(),
                            ItemBuilder::init(
                                'T-Shirt',
                                MoneyBuilder::init(
                                    'USD',
                                    '5.00'
                                )->build(),
                                '1'
                            )
                                ->tax(MoneyBuilder::init(
                                    'USD',
                                    '0.00'
                                )->build())
                                ->sku('1457432')
                                ->build()
                        ])
                        ->shipping(
                            ShippingDetailsBuilder::init()
                                ->address(
                                    AddressBuilder::init(
                                        'US'
                                    )
                                        ->addressLine1('123 Main Street')
                                        ->adminArea1('CA')
                                        ->adminArea2('San Jose')
                                        ->postalCode('95131')
                                        ->build()
                                )->build()
                        )->build()
                ]
            )
                ->paymentSource(
                    PaymentSourceBuilder::init()
                        ->paypal(
                            PayPalWalletBuilder::init()
                                ->experienceContext(
                                    PayPalWalletExperienceContextBuilder::init()
                                        ->locale('en-US')
                                        ->returnUrl('https://example.com/returnUrl')
                                        ->cancelUrl('https://example.com/cancelUrl')
                                        ->landingPage(PayPalExperienceLandingPage::LOGIN)
                                        ->build()
                                )
                                ->build()
                        )
                        ->build()
                )
                ->build(),
            'payPalRequestId' => strval(rand(1, 99999999999)),
            'prefer' => 'return=representation'
        ];

        $response = self::$ordersController->ordersCreate($request);

        $this->assertTrue($response->isSuccess());
        $this->assertEquals(200, $response->getStatusCode());

        return $response->getResult();
    }

    /**
     * @depends testCreateOrder
     */
    public function testPatchOrder(Order $orderDetail): void
    {
        $request = [
            'id' => $orderDetail->getId(),
            'body' => [
                PatchBuilder::init(
                    PatchOp::REPLACE
                )
                    ->path("/purchase_units/@reference_id==d'default'/shipping/address")
                    ->value([
                        'address_line_1' => '1234 Main St',
                        'address_line_2' => 'Floor 6',
                        'admin_area_2' => 'San Francisco',
                        'admin_area_1' => 'CA',
                        'postal_code' => '94107',
                        'country_code' => 'US',
                    ])
                    ->build()
            ]
        ];

        $response = self::$ordersController->ordersPatch($request);

        $this->assertTrue($response->isSuccess());
        $this->assertEquals(204, $response->getStatusCode());
    }

    /**
     * @depends testCreateOrder
     * @depends testPatchOrder
     */
    public function testGetOrderDetails(Order $orderDetail): void
    {
        $request = [
            'id' => $orderDetail->getId()
        ];

        $response = self::$ordersController->ordersGet($request);

        $this->assertTrue($response->isSuccess());
        $this->assertEquals(200, $response->getStatusCode());

        $this->assertEquals(
            '1234 Main St',
            $response->getResult()
                ->getPurchaseUnits()[0]
                ->getShipping()
                ->getAddress()
                ->getAddressLine1()
        );
    }
}
