<?php
require_once '../_plugins/medoo.php';
require_once '../settings.php';

$mdb = new medoo( [
  'database_type' => 'mysql',
  'database_name' => $settings['db']['database'],
  'server' => $settings['db']['host'] ,
  'username' => $settings['db']['user'],
  'password' => $settings['db']['password'],
  'charset' => 'utf8'
] );

$url = "https://smartforms.ekomi.com/api/v3/answers?form_id=494999&page=1";

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = array(
   "interface-id: 154480",
   "interface-password: 1f82bae5daf2ee2275540cef9",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$resp = curl_exec($curl);
curl_close($curl);
$result = json_decode($resp, true );

if ( $result['data']['status'] == 'success' )
{
  foreach ( $result['data']['result'] as $row )
  {
    $data['id'] = $row['id'];
    $data['shop_id'] = $row['shop_id'];
    $data['order_id'] = $row['order_id'];
    $data['date_add'] = date( 'Y-m-d H:i:s', $row['published_at'] );

    foreach ( $row['forms'] as $form )
    {
      if ( $form['form_id'] == 494999 )
      {
        $data['product_id'] = $form['product_id'];
        foreach ( $form['questions'] as $question )
        {
          if ( $question['question_id'] == '2705526' )
          {
            $data['rating'] = $question['mapped_value'];
          }

          if ( $question['question_id'] == '2705527' )
          {
            $data['review'] = $question['answer_value'];
          }

          if ( $question['question_id'] == '2714943' )
          {
            $data['age'] = (int)$question['answer_value'];
          }

          if ( $question['question_id'] == '2714942' )
          {
            if ( $question['answer_value']['options'][0]['option_label'] == 'Tak' )
            {
              $data['recommendation'] = 1;
            }
            else
            {
              $data['recommendation'] = 0;
            }
          }
        }

        if ( !$mdb -> count( 'pakanaturypl', [ 'external_id' => $row['id'] ] ) )
        {
          // dodaję do bazy danych
          $mdb -> insert( 'pakanaturypl', [
            'external_id' => $data['id'],
            'order_id' => $data['order_id'],
            'product_id' => $data['product_id'],
            'rating' => $data['rating'],
            'review' => $data['review'],
            'age' => $data['age'] > 0 ? $data['age'] : null,
            'recommendation' => $data['recommendation'],
            'date_add' => $data['date_add']
          ] );

          $insert_id = $mdb -> id();
          if ( !$insert_id )
          {
            echo '<pre>';
            print_r( $mdb -> error() );
            echo '</pre>';
            exit;
          }
          else
          {
            echo '<p>Dodano opinię: ' . $insert_id . '</p>';
          }
        }
      }
    }
  }
}
?>