<?php

namespace App\Http\Controllers\Network\Classes;

use Exception;
use GuzzleHttp\Client;


class Booking implements SalesGetterApi
{

    public $api_key;
    public $affiliate_id;
    public $website_id;
    public $client;
    public $sales;

    public function __construct($api_key, $affiliate_id, $website_id = null)
    {
        $this->api_key = $api_key;
        $this->affiliate_id = $affiliate_id;
        $this->website_id = $website_id;
        $this->client = new Client();
        $this->sales = [];
    }

    public function getSales($start_days, $end_days)
    {
        return []; //TODO: to implement the api after integration
        $query = [
            'affiliate_id' => $this->affiliate_id,
            'api_key' => $this->api_key,
            'offer_id' => 0,
            'start_at_row' => 1,
            'row_limit' => 0,
            'start_date' => date('Y-m-d', strtotime("today - {$start_days} days ")),
            'end_date' => date('Y-m-d', strtotime("tomorrow - {$end_days} days "))
        ];

        try{
            $salesResponse = $this->client->request(
                'GET',
                'https://affiliate.jumia.com/affiliates/api/Reports/Conversions',
                [
                    'query' => $query
                ]
            );

            $sales = json_decode($salesResponse->getBody());
            if(!$sales) return;

            $this->sales = $sales;

        }

        catch(Exception $e){

        }
    }
}
