api_key = get_option('addtaisa_api_key', ''); } private function request(string $method, string $endpoint, array $body = []): array|WP_Error { if (empty($this->api_key)) { return new WP_Error('no_api_key', 'Clé API non configurée'); } $args = [ 'method' => $method, 'headers' => [ 'X-API-Key' => $this->api_key, 'Content-Type' => 'application/json', ], 'timeout' => 15, ]; if (!empty($body) && in_array($method, ['POST', 'PATCH', 'PUT'])) { $args['body'] = wp_json_encode($body); } $url = ADDTAISA_API_URL . '/v1' . $endpoint; $response = wp_remote_request($url, $args); if (is_wp_error($response)) { return $response; } $code = wp_remote_retrieve_response_code($response); $data = json_decode(wp_remote_retrieve_body($response), true); if ($code >= 400) { $msg = $data['error']['message'] ?? 'Erreur API (code ' . $code . ')'; return new WP_Error('api_error', $msg); } return $data['data'] ?? $data ?? []; } public function get(string $endpoint): array|WP_Error { return $this->request('GET', $endpoint); } public function post(string $endpoint, array $body = []): array|WP_Error { return $this->request('POST', $endpoint, $body); } public function patch(string $endpoint, array $body = []): array|WP_Error { return $this->request('PATCH', $endpoint, $body); } public function delete(string $endpoint): array|WP_Error { return $this->request('DELETE', $endpoint); } // ── Convenience methods ── public function get_analytics_overview(string $period = '30d'): array|WP_Error { return $this->get('/analytics/overview?period=' . $period); } public function get_conversations(int $page = 1, int $per_page = 20): array|WP_Error { return $this->get("/conversations?page={$page}&per_page={$per_page}"); } public function get_contacts(int $page = 1, int $per_page = 20, string $search = ''): array|WP_Error { $qs = "?page={$page}&per_page={$per_page}"; if ($search) $qs .= '&search=' . urlencode($search); return $this->get('/contacts' . $qs); } public function get_knowledge(): array|WP_Error { return $this->get('/knowledge'); } public function create_knowledge(array $data): array|WP_Error { return $this->post('/knowledge', $data); } public function update_knowledge(string $id, array $data): array|WP_Error { return $this->patch("/knowledge/{$id}", $data); } public function delete_knowledge(string $id): array|WP_Error { return $this->delete("/knowledge/{$id}"); } public function seed_knowledge_defaults(): array|WP_Error { return $this->post('/knowledge/seed-defaults'); } public function get_bot_config(): array|WP_Error { $merchant = $this->get('/merchants/me'); if (is_wp_error($merchant)) return $merchant; return $merchant['bot_config'] ?? []; } public function update_bot_config(array $data): array|WP_Error { return $this->patch('/merchants/me/bot-config', $data); } public function get_widget_config(): array|WP_Error { $merchant = $this->get('/merchants/me'); if (is_wp_error($merchant)) return $merchant; return $merchant['widget_config'] ?? []; } public function update_widget_config(array $data): array|WP_Error { return $this->patch('/merchants/me/widget-config', $data); } public function get_promo_config(): array|WP_Error { $merchant = $this->get('/merchants/me'); if (is_wp_error($merchant)) return $merchant; return $merchant['promo_config'] ?? []; } public function update_promo_config(array $data): array|WP_Error { return $this->patch('/merchants/me/promo-config', $data); } public function get_stores(): array|WP_Error { return $this->get('/stores'); } public function create_store(array $data): array|WP_Error { return $this->post('/stores', $data); } public function delete_store(string $id): array|WP_Error { return $this->delete("/stores/{$id}"); } public function get_billing_usage(): array|WP_Error { return $this->get('/billing/usage'); } public function get_product_stats(): array|WP_Error { return $this->get('/catalog/products/stats'); } }