<?php

$PROJECT_TITLE = 'Portfolio - mschwab.net';
$MAIL = 'ms@mschwab.net';
$MOBILE = '+41 78 819 76 00';

$WEB_URL = 'https://www.mschwab.net';
$IMMICH_URL = 'https://immich.mschwab.net';
$IMMICH_API_KEY  = 'xzANIBPSjx7iHJTK6OKV2yLyDPy9Vh4NAKnCJ3t4U';

function immichRequest($url) {

    global $IMMICH_API_KEY;

    $ch = curl_init($url);
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => [
            "x-api-key: $IMMICH_API_KEY",
            "Accept: application/json"
        ]
    ]);

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

    return json_decode($response, true);
}

function getThumbnailBase64($assetId) {
  
    global $IMMICH_URL, $IMMICH_API_KEY;

    $ch = curl_init("$IMMICH_URL/api/assets/$assetId/thumbnail?size=thumbnail");
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => [
            "x-api-key: $IMMICH_API_KEY"
        ]
    ]);

    $image = curl_exec($ch);
    $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
    curl_close($ch);

    return "data:$contentType;base64," . base64_encode($image);
}

function logData(string $message, ?array $data = null) {
  $logDir = '../logs';
  $today = date('Y-m-d');
  $now = date('Y-m-d H:i:s');

  $date = new DateTime("now", new DateTimeZone('Europe/Zurich')); 

  if (!is_dir($logDir)) {
    mkdir($logDir, 0777, true);
  }
  $logFile = $logDir . '/log-' . $date->format('Y-m-d') . '.log';

  $logData = '[' . $date->format('Y-m-d H:i:s') . ']' . ' - ' . $message . "\n";

  if ($data) {
    $dataString = print_r($data, true) . "\n";
    $logData .= $dataString;
  }

  file_put_contents($logFile, $logData, FILE_APPEND);
}