<?php

namespace App\Repositories;

use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use App\Exceptions\GeneralException;
use App\Repositories\BaseRepository;
use App\Models\Booking;


/**
 * Class SimplyBookRepository.
 */
class SimplyBookRepository extends BaseRepository
{
    protected $baseUrl;
    protected $company;
    protected $login;
    protected $password;

    /**
     * BookingController constructor.
     *
     * @param model $model
     * @param testCenterModel $testCenterModel
     */
    public function __construct()
    {
        $this->baseUrl = 'https://user-api-v2.simplybook.pro/admin/';
        $this->company = 'pharmaclone';
        $this->login = 'testcenter@pharmause.com';
        $this->password = 'Hoppahoppa00th!';
    }

    /**
     * @return string
     */
    public function model()
    {
        return Booking::class;
    }

    /**
     * @param array $data
     *
     * @return \Illuminate\Database\Eloquent\Model|mixed
     * @throws \Exception
     * @throws \Throwable
     */
    /**
     * @param string $device_token
     * @param int $mobile_number
     * @param string $circle_code
     *
     * @return array|bool
     * @throws GeneralException
     */
    public function authentication()
    {
        $url = $this->baseUrl.'auth';
        $ch = curl_init($url);
        $data = [
            "company"=>$this->company,
            "login"=>$this->login,
            "password"=>$this->password
        ];
        $payload = json_encode($data);
        $header = array(
            'Content-Type:application/json'
        );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
        curl_close($ch);         
        $output = (array)json_decode($response);
        //print_r($output);die;
        if($output){
            if(isset($output['token'])){
                return $output['token'];
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * @param array $data
     *
     * @return \Illuminate\Database\Eloquent\Model|mixed
     * @throws \Exception
     * @throws \Throwable
     */
    /**
     * @param string $device_token
     * @param int $mobile_number
     * @param string $circle_code
     *
     * @return array|bool
     * @throws GeneralException
     */
    public function booking($token = '', $page, $on_page, $date, $providers = 2)
    {
        $url = $this->baseUrl."bookings?page=$page&on_page=$on_page&filter[date]=$date&filter[providers]=$providers";
		//echo $url;
        $ch = curl_init($url);
        $header = array(
            'Content-Type:application/json',
            'X-Company-Login:'.$this->company,
            'X-Token:'.$token
        );
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
        curl_close($ch);         
        $output = (array)json_decode($response, true);
        //print_r($output);die;
        if(count($output)>0){
            return $output;
        } else {
            return false;
        }
    }

    /**
     * @param array $data
     *
     * @return \Illuminate\Database\Eloquent\Model|mixed
     * @throws \Exception
     * @throws \Throwable
     */
    /**
     * @param string $device_token
     * @param int $mobile_number
     * @param string $circle_code
     *
     * @return array|bool
     * @throws GeneralException
     */
    public function payment($token = '', $invoice_id = 0)
    {
        $url = $this->baseUrl."invoices/$invoice_id/accept-payment";
        $ch = curl_init($url);
        $header = array(
            'Content-Type:application/json',
            'X-Company-Login:'.$this->company,
            'X-Token:'.$token
        );
		$data = [
            "payment_processor"=>"manual"
        ];
        $payload = json_encode($data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
        curl_close($ch);         
        $output = (array)json_decode($response, true);
       // print_r($output);die;
        if(count($output)>0){
            if($output['status'] == 'paid'){
                return $output;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
}
