<?php
class BukkuProduct{
  
    // database connection
    private $conn;
    
    //api properties
    private $bearerToken;
    private $baseUrl;
    
    //default course add
    public $input;
    
    //both default & custom
    public $sku;
    
    //custom price course add
    public $courseName;
    public $programGroupID;
    public $price;
    public $trialPrice;
        
    // constructor with $bBT as bearerToken
    public function __construct($bBT, $bUrl, $db){
        $this->bearerToken = $bBT;
        $this->baseUrl = $bUrl;
        $this->conn = $db;
    }
    
    function addData(){
        
        if(!empty($this->input->trialPrice)){
            return array (
                    'name' => $this->input->courseName,
                    'sku' => $this->sku,
                    'is_selling' => true,
                    'sale_account_id' => 20,
                    'is_buying' => false,
                    'track_inventory' => false,
                    'units' => [
                        [
                            "label"=> "session",
                            "rate" => 1,
                            "sale_price" => $this->input->price,
                            "is_base"=>true,
                            "is_sale_default"=> true
                        ],
                        [
                            "label" => "session",
                            "rate" => 1,  
                            "sale_price" => $this->input->trialPrice,
                        ]
                    ],
                    'groupIds' => [
                        $this->input->programGroupID
                    ]
            );
        }else{
            return array (
                    'name' => $this->input->courseName,
                    'sku' => $this->sku,
                    'is_selling' => true,
                    'sale_account_id' => 20,
                    'is_buying' => false,
                    'track_inventory' => false,
                    'units' => [
                        [
                            "label"=> "session",
                            "rate" => 1,
                            "sale_price" => $this->input->price,
                            "is_base"=>true,
                            "is_sale_default"=> true
                        ]
                    ],
                    'group_ids' => [
                        $this->input->programGroupID
                    ]
            );
        }
    }

    function createProduct(){
        
        $endpoint = '/products';
        $apiUrl = $this->baseUrl . $endpoint;
        
        $data = $this->addData();
        
        $jsonData = json_encode($data, JSON_PRETTY_PRINT);
        
        // Initialize cURL session
        $ch = curl_init($apiUrl);
        
        if ($ch === false) {
            echo 'Failed to initialize cURL session.';
        }
    
        // Set cURL options
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
        // Define headers
        $headers = array(
            "Authorization: Bearer " . $this->bearerToken,
            'Content-Type: application/json',
            'Accept: application/json', // Adding Accept header
            'Company-Subdomain: swjuniors' // Adding custom header
        );
        
        // Set the headers
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        
        // Enable verbose mode
        curl_setopt($ch, CURLOPT_VERBOSE, true);
        $verbose = fopen('php://temp', 'w+');
        curl_setopt($ch, CURLOPT_STDERR, $verbose);
    
        // Execute cURL request and get response
        $response = curl_exec($ch);
        
        // Check HTTP status code
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        
        if ($response === false) {
            echo 'cURL Error: ' . curl_error($ch);
            rewind($verbose);
            $verboseLog = stream_get_contents($verbose);
            echo "Verbose information:\n" . $httpCode;
            return false;
        } else {
            if ($httpCode >= 200 && $httpCode < 300) {
                $responseData = json_decode($response, true);
                echo 'Response: ' . $response;
                return $response;
            } else {
                echo 'HTTP Status Code: ' . $httpCode . "\n";
                $responseData = json_decode($response, true);
                if (isset($responseData['error'])) {
                    echo 'Error Message: ' . $responseData['error'];
                } else {
                    echo 'Error: ' . $response;
                }
                return false;
            }
        }
        
        // Close cURL session
        curl_close($ch);
    }
    
    function addCustomPriceData(){
        
        if(!empty($this->$trialPrice)){
            return array (
                'name' => $this->courseName,
                'sku' => $this->sku,
                'is_selling' => true,
                'sale_account_id' => 20,
                'is_buying' => false,
                'track_inventory' => false,
                'units' => [
                    [
                        "label"=> "session",
                        "rate" => 1,
                        "sale_price" => $this->price,
                        "is_base"=>true,
                        "is_sale_default"=> true
                    ],
                    [
                        "label" => "session",
                        "rate" => 1,
                        "sale_price" => $this->trialPrice,
                    ]
                ],
                'group_ids' => [
                    $this->programGroupID
                ]
            );
        }else{
            return array (
                'name' => $this->courseName,
                'sku' => $this->sku,
                'is_selling' => true,
                'sale_account_id' => 20,
                'is_buying' => false,
                'track_inventory' => false,
                'units' => [
                    [
                        "label"=> "session",
                        "rate" => 1,
                        "sale_price" => $this->price,
                        "is_base"=>true,
                        "is_sale_default"=> true
                    ]
                ],
                'group_ids' => [
                    $this->programGroupID
                ]
            );
        }
    }
    
    function createCustomPriceProduct(){
        
        $endpoint = '/products';
        $apiUrl = $this->baseUrl . $endpoint;
        
        $data = $this->addCustomPriceData();
        
        $jsonData = json_encode($data, JSON_PRETTY_PRINT);
        
        // Initialize cURL session
        $ch = curl_init($apiUrl);
        
        if ($ch === false) {
            echo 'Failed to initialize cURL session.';
        }
        
        // Set cURL options
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
        // Define headers
        $headers = array(
            "Authorization: Bearer " . $this->bearerToken,
            'Content-Type: application/json',
            'Accept: application/json', // Adding Accept header
            'Company-Subdomain: swjuniors' // Adding custom header
        );
        
        // Set the headers
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        
        // Enable verbose mode
        curl_setopt($ch, CURLOPT_VERBOSE, true);
        $verbose = fopen('php://temp', 'w+');
        curl_setopt($ch, CURLOPT_STDERR, $verbose);
        
        // Execute cURL request and get response
        $response = curl_exec($ch);
        
        // Check HTTP status code
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        
        if ($response === false) {
            echo 'cURL Error: ' . curl_error($ch);
            rewind($verbose);
            $verboseLog = stream_get_contents($verbose);
            echo "Verbose information:\n" . $httpCode;
            return false;
        } else {
            if ($httpCode >= 200 && $httpCode < 300) {
                $responseData = json_decode($response, true);
                return $response;
            } else {
                echo 'HTTP Status Code: ' . $httpCode . "\n";
                $responseData = json_decode($response, true);
                if (isset($responseData['error'])) {
                    echo 'Error Message: ' . $responseData['error'];
                } else {
                    echo 'Error: ' . $response;
                }
                return false;
            }
        }
        
        // Close cURL session
        curl_close($ch);
    }

}

?>