<?php
namespace App\Controller;
use App\Service\PlanityService;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use GuzzleHttp\Client;
class PlanityWebSocketController extends AbstractController
{
#[Route('/admin/search-clients-planity', name: 'search_clients', methods: ['GET'])]
public function searchClientsFromPlanity(Request $request): JsonResponse
{
try {
$searchTerm = $request->query->get('q');
$client = new Client();
$response = $client->post(
'https://day79mubw3-dsn.algolia.net/1/indexes/business_customers/query',
[
'headers' => [
'accept' => 'application/json',
'content-type' => 'application/x-www-form-urlencoded',
'x-algolia-agent' => 'Algolia for JavaScript (3.35.1); Browser',
'x-algolia-application-id' => 'DAY79MUBW3',
'x-algolia-api-key' => 'YjE0OWQzYWNkYzNkNThjMDM1MTk1MTMzZmM0NzQ5YzdkYmI0YjAzNDhiZThhM2RhNGEzNjA5NWJhZjdkNjQ1NGZpbHRlcnM9YnVzaW5lc3NJZCUzQS1Oak1aem5iVnBHZmpwR2M2aDho'
],
'body' => json_encode([
'params' => "query={$searchTerm}&hitsPerPage=5"
])
]
);
$data = json_decode((string) $response->getBody(), true);
return new JsonResponse($data);
} catch (\Exception $e) {
return new JsonResponse(['error' => $e->getMessage()], 500);
}
}
#[Route('/admin/getTelFromPlanity', name: 'get_tel_from_planity', methods: ['GET'])]
public function getTelCustomer(PlanityService $planityService): JsonResponse
{
return $planityService->getPlanning(date('Y-m-d', strtotime('now -1 day')));
}
#[Route('/admin/all-clients-planity', name: 'get_all_clients', methods: ['GET'])]
public function getAllClientsFromPlanity(): JsonResponse
{
try {
$client = new Client();
$allClients = [];
$page = 0;
$hitsPerPage = 1000;
do {
$response = $client->post(
'https://day79mubw3-dsn.algolia.net/1/indexes/business_customers/query',
[
'headers' => [
'accept' => 'application/json',
'content-type' => 'application/x-www-form-urlencoded',
'x-algolia-agent' => 'Algolia for JavaScript (3.35.1); Browser',
'x-algolia-application-id' => 'DAY79MUBW3',
'x-algolia-api-key' => 'YjE0OWQzYWNkYzNkNThjMDM1MTk1MTMzZmM0NzQ5YzdkYmI0YjAzNDhiZThhM2RhNGEzNjA5NWJhZjdkNjQ1NGZpbHRlcnM9YnVzaW5lc3NJZCUzQS1Oak1aem5iVnBHZmpwR2M2aDho'
],
'body' => json_encode([
'params' => "query=&page={$page}&hitsPerPage={$hitsPerPage}&filters=deletedAt=0&attributesToHighlight=[]&attributesToRetrieve=[\"id\",\"name\",\"phone\",\"email\",\"deletedAt\",\"businessId\"]"
])
]
);
$data = json_decode((string) $response->getBody(), true);
$allClients = array_merge($allClients, $data['hits']);
$page++;
} while ($page < $data['nbPages']);
$uniqueClients = [];
$seenCombinations = [];
$duplicates = [];
foreach ($allClients as $hit) {
$email = strtolower($hit['email'] ?? '');
$fullName = strtolower(trim($hit['name'] ?? ''));
// Créer une clé unique basée sur email ET nom complet
$comparisonKey = $email . '|' . $fullName;
// Si pas d'email, on skip
if (empty($email)) {
continue;
}
if (isset($seenCombinations[$comparisonKey])) {
if (!isset($duplicates[$comparisonKey])) {
$duplicates[$comparisonKey] = [
'count' => 1,
'data' => $hit
];
}
$duplicates[$comparisonKey]['count']++;
continue;
}
$seenCombinations[$comparisonKey] = true;
// Séparation du prénom et nom
$nameParts = explode(' ', $hit['name']);
$firstName = $nameParts[0] ?? '';
$lastName = isset($nameParts[1]) ? implode(' ', array_slice($nameParts, 1)) : '';
$uniqueClients[] = [
'id' => $hit['id'] ?? '',
'email' => $email,
'firstName' => $firstName,
'lastName' => $lastName,
'phone' => $hit['phone'] ?? ''
];
}
return new JsonResponse([
'statistiques' => [
'total_initial' => count($allClients),
'total_unique' => count($uniqueClients),
'nombre_doublons' => count($duplicates),
'exemples_doublons' => array_slice($duplicates, 0, 5, true)
],
'clients' => $uniqueClients
]);
} catch (\Exception $e) {
return new JsonResponse(['error' => $e->getMessage()], 500);
}
}
}