<?php

namespace App\Console\Commands;

use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Yajny\EventsYj\Library\Boostiny\Conversion;
use Yajny\Member\Models\BoostinyClick;
use Yajny\Models\Store;

class BoostinyOrdersBulk extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'boostiny:bulk ';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Send All Orders to Boostiny API';

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle()
    {
        $boostinyOrders = BoostinyClick::where("sended_to_boostiny",false)->limit(5000)->get();
        foreach ($boostinyOrders as $boostinyOrder){
            $region_code = $boostinyOrder->region_code;
            switch ($boostinyOrder->currency) {
                case "SAR":
                    $region_code = 'sa';
                    break;
                case "EGP":
                    $region_code = 'eg';
                    break;
                case "AED":
                    $region_code = 'ae';
                    break;
            }
            $boostinyClickId=$boostinyOrder->boostiny_click_id;
            $orderID=$boostinyOrder->order_id;
            $orderDetails=DB::table("sales")->where('click_id',$boostinyOrder->click_id)
                ->join('stores', 'sales.store_id', '=', 'stores.id')
                ->select('sales.id', 'stores.slug','sales.currency','sales.comission', 'sales.currency','sales.order_id')
                ->get();
            $total_amounts = 0;
            if (count($orderDetails)>0) {
                $total_amounts = 0;
                foreach ($orderDetails as $orderDetail) {
                    $total_amounts += $orderDetail->comission;

                }
            }
            if ($total_amounts>0){
                $countries = ['eg' => 'EGY', 'ae' => 'ARE', 'kw' => 'KWT', 'sa' => 'SAU'];
                $client_id = '';
                $country = '';
                $store_slug=$orderDetail->slug;
                $this->output->write($store_slug,true);
                if ($store_slug == 'amazon') {
                    $client_id = 'amazon-egypt';
                    $api_key = 'n9S6-suBKXwSCoTSvwvSXMOVbJA2HR0GgKdRltSdKNA';
                    $country = 'EGY';
                }
                if ($store_slug == 'amazon-sa') {
                    $client_id = 'amazon-ksa';
                    $api_key = 'jnp0q902OYNRrKenCmYnY_1EL4T3aBrW0OCGjssmn34';
                    $country = 'SAU';
                }
                if ($store_slug == 'aliexpress') {
                    $client_id = 'aliexpress1';
                    $api_key = 't8YysyKSx8cbBzG-d2jjmXASrFKzmTEQlpERJOOVqk4';
                    $country = '';
                }
                $currency = 'USD';
                //$this->output->write("this is the time ".$boostinyOrder->created_at,true);
                $usedDate=$boostinyOrder->created_at->format('Y-m-d H:i:s');
                $query = [
                    'client' => $client_id,
                    'country' => $country,
                    'currency' => $currency,
                    'click_id' => $boostinyClickId,
                    'order_id' => $boostinyOrder->click_id,
                    'amount' => $total_amounts,
                    'created_at' => $usedDate,
                ];
                $client = new Client();
                $request = $client->request(
                    'GET',
                    'https://track.arabytracking.net/link/conversion',
                    [
                        'query' => $query,
                        'headers' => [
                            'Authorization' => $api_key,
                        ],
                    ]
                );
                $response=json_decode($request->getBody(),true);
                $this->output->write("-----------------------------------",true);
                $this->output->write( json_encode($response),true);

                if ($response['result']=="ok"){
                    $boostinyOrder->sended_to_boostiny=true;
                    $boostinyOrder->save();
                }
            }
            $boostinyOrder->sended_to_boostiny=true;
            $boostinyOrder->save();

            }




    }
}
