<?php

declare(strict_types=1);

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

namespace PayPalRESTAPIsLib\Tests\E2E;

use PayPalRESTAPIsLib\Controllers\OrdersController;
use PayPalRESTAPIsLib\Models\Builders\AddressBuilder;
use PayPalRESTAPIsLib\Models\Builders\AmountBreakdownBuilder;
use PayPalRESTAPIsLib\Models\Builders\AmountWithBreakdownBuilder;
use PayPalRESTAPIsLib\Models\Builders\ItemBuilder;
use PayPalRESTAPIsLib\Models\Builders\MoneyBuilder;
use PayPalRESTAPIsLib\Models\Builders\OrderRequestBuilder;
use PayPalRESTAPIsLib\Models\Builders\PatchBuilder;
use PayPalRESTAPIsLib\Models\Builders\PaymentSourceBuilder;
use PayPalRESTAPIsLib\Models\Builders\PayPalWalletBuilder;
use PayPalRESTAPIsLib\Models\Builders\PayPalWalletExperienceContextBuilder;
use PayPalRESTAPIsLib\Models\Builders\PurchaseUnitRequestBuilder;
use PayPalRESTAPIsLib\Models\Builders\ShippingDetailsBuilder;
use PayPalRESTAPIsLib\Models\CheckoutPaymentIntent;
use PayPalRESTAPIsLib\Models\Order;
use PayPalRESTAPIsLib\Models\PatchOp;
use PayPalRESTAPIsLib\Models\PayPalExperienceLandingPage;
use PayPalRESTAPIsLib\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()
        );
    }
}
