<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Paypal extends Model
{
    protected $table = 'paypal';
    protected $guarded = [];
    public $timestamps = false;

    public static function createBody(array $items, array $amount, array $shipping = [], $type = 'paypal'): array
    {
        switch ($type) {
            case 'paypal':
                $shippingPreference = 'GET_FROM_FILE';
                break;
            case 'google':
                $shippingPreference = 'GET_FROM_FILE';
                break;
            case 'form':
                $shippingPreference = 'SET_PROVIDED_ADDRESS';
                break;
            default:
                $shippingPreference = 'GET_FROM_FILE';
        }


        $body = [
            'intent' => 'CAPTURE',
//            'payment_source' => [
//                'paypal' => [
//                    'experience_context' => [
//                        'brand_name' => config('paypal.brand_name'),
//                    ]
//                ],
//
//            ],
            'application_context' => [
                'return_url' => url("api/capture-order?type=$type"),
                'cancel_url' => url('paypal/cancel-order'),
                'brand_name' => config('paypal.brand_name'),
                'locale' => config('paypal.locale_bcp_47'),
                'landing_page' => 'BILLING',
                'user_action' => 'PAY_NOW',
                'shipping_preference' => $shippingPreference,
            ],
            'purchase_units' => [
                0 => [
                    'reference_id' => 'PUHF',
                    'description' => 'Sporting Goods',
                    'custom_id' => 'CUST-HighFashions',
                    'soft_descriptor' => 'HighFashions',
                    'amount' => $amount,
                    'items' => $items,
                ]
            ],
        ];
        if ($shipping) {
            $body['purchase_units'][0]['shipping'] = $shipping;
        }
        return $body;
    }

}
