<?php
$sheetId      = 160662231666564;
$attachmentId = 7929651976687492;

// //get job sheet
// $ch = curl_init();
// curl_setopt($ch, CURLOPT_URL, "https://api.smartsheet.com/2.0/sheets/$sheetId");
// curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: Bearer qJc6jnLsHoqznwCbkApNwLMQamt6ITcykpgen", 'Content-Type: application/json']);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// $sheet = json_decode(curl_exec($ch));
// curl_close($ch);

// //find daily log row id
// foreach ( $sheet->rows as $row ) {
//     foreach ( $row->cells as $cell ) {
//         if ( strtolower($cell->value) == 'daily log' ) {
//             $rowId = $row->id;
//             break;
//         }
//     }
// }

// //get discussion using rowId
// $ch = curl_init();
// curl_setopt($ch, CURLOPT_URL, "https://api.smartsheet.com/2.0/sheets/$sheetId/rows/$rowId/discussions?include=comments,attachments");
// curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: Bearer qJc6jnLsHoqznwCbkApNwLMQamt6ITcykpgen", 'Content-Type: application/json']);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// $discussions = json_decode(curl_exec($ch));
// curl_close($ch);

// //build array and save to db
// $i = 0;
// $array   = [];
// $dates   = [];
// $weather = [];
// foreach ( $discussions->data as $discussion ) {
//     foreach ( $discussion->comments as $comment ) {
//         // $array[$i]['id']             = $comment->id;
//         $array[$i]['ssCommentId']    = $comment->id;
//         $array[$i]['projectId']      = $project['id'];
//         $array[$i]['text']           = $comment->text;
//         $array[$i]['createdByName']  = $comment->createdBy->name;
//         $array[$i]['createdByEmail'] = $comment->createdBy->email;
//         $array[$i]['createdAt']      = $comment->createdAt;
//         $array[$i]['sheetId']        = $sheetId;
//         $array[$i]['rowId']          = $rowId;

//         $dates[] = date('Y-m-d', strtotime($comment->createdAt));

//         // if ( $exists = $db->cell("SELECT id FROM comment WHERE id = ?", $array[$i]['id']) ) {
//         if ( $exists = $db->cell("SELECT id FROM comment WHERE ssCommentId = ?", $array[$i]['ssCommentId']) ) {
//             $db->update('comment', $array[$i], [
//                 'ssCommentId' => $array[$i]['ssCommentId']
//             ]);
//         } else {
//             $db->insert('comment', $array[$i]);
//         }
        
//         $k = 0;
//         foreach ( $comment->attachments as $attachment ) {
//             // $array[$i]['attachments'][$k]['id'] = $attachment->id;
//             $array[$i]['attachments'][$k]['ssAttachmentId'] = $attachment->id;
//             $array[$i]['attachments'][$k]['commentId']      = $db->cell("SELECT id FROM comment WHERE ssCommentId = ?", $comment->id);
//             // $array[$i]['attachments'][$k]['commentId']      = $comment->id;
//             $array[$i]['attachments'][$k]['ssCommentId']    = $comment->id;
//             $array[$i]['attachments'][$k]['projectId']      = $project['id'];
//             $array[$i]['attachments'][$k]['name']           = $attachment->name;
//             $array[$i]['attachments'][$k]['mimeType']       = $attachment->mimeType;
//             $array[$i]['attachments'][$k]['createdAt']      = $attachment->createdAt;
//             $array[$i]['attachments'][$k]['createdByName']  = $attachment->createdBy->name;
//             $array[$i]['attachments'][$k]['createdByEmail'] = $attachment->createdBy->email;

//             $attachmentId = $attachment->id;

//             //get attachment url
//             $ch = curl_init();
//             curl_setopt($ch, CURLOPT_URL, "https://api.smartsheet.com/2.0/sheets/$sheetId/attachments/$attachmentId");
//             curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: Bearer qJc6jnLsHoqznwCbkApNwLMQamt6ITcykpgen", 'Content-Type: application/json']);
//             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//             $response = json_decode(curl_exec($ch));
//             curl_close($ch);

//             $k++;
//         }
//         $i++;
//     }
// }
// echo "<pre>";
// print_r($array);
// echo "</pre>";

//get attachment url
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.smartsheet.com/2.0/sheets/$sheetId/attachments/$attachmentId");
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: Bearer qJc6jnLsHoqznwCbkApNwLMQamt6ITcykpgen", 'Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = json_decode(curl_exec($ch));
curl_close($ch);

echo "<pre>";
print_r($response);
echo "</pre>";

$file = file_get_contents($response->url);
file_put_contents("../../assets/images/attachments/$attachmentId-".$response->name, $file);