prepare("SELECT * FROM google_tokens WHERE praticien_id = ?"); $stmt->execute([$praticien_id]); $row = $stmt->fetch(); if (!$row) { error_log("❌ Aucun token trouvé pour praticien ID $praticien_id"); return false; } // 2. Préparer le client Google $client = new Client(); $client->setClientId($config['client_id']); $client->setClientSecret($config['client_secret']); $client->setAccessToken([ 'access_token' => $row['access_token'], 'expires_in' => 3600, 'refresh_token' => $row['refresh_token'], 'created' => $row['expires_at'] - 3600 ]); // 3. Rafraîchir le token si expiré if ($client->isAccessTokenExpired()) { $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); $new_token = $client->getAccessToken(); $stmt = $pdo->prepare("UPDATE google_tokens SET access_token = ?, expires_at = ? WHERE praticien_id = ?"); $stmt->execute([ $new_token['access_token'], time() + $new_token['expires_in'], $praticien_id ]); } // 4. Calculer l'heure de fin si elle n'est pas transmise if (!$end_iso) { $start = new DateTime($start_iso); $start->modify("+{$duree_minutes} minutes"); $end_iso = $start->format(DateTime::ATOM); } // 🔍 Log pour debug (facultatif) error_log("🟢 Google Sync - START: $start_iso"); error_log("🟢 Google Sync - END : $end_iso"); // 5. Préparer le service et l'événement $calendar = new Calendar($client); $event = new Event([ 'summary' => $summary, 'description' => $description, 'start' => [ 'dateTime' => $start_iso, 'timeZone' => 'Europe/Paris' ], 'end' => [ 'dateTime' => $end_iso, 'timeZone' => 'Europe/Paris' ] ]); try { $calendar->events->insert('primary', $event); return true; } catch (Exception $e) { error_log("❌ Erreur Google Calendar : " . $e->getMessage()); return false; } }