<?php

namespace App\Services\Integrations;

use App\Models\ApiRequestLog;
use App\Models\PlatformConnection;

class ApiRequestLogger
{
    public function log(PlatformConnection $connection, string $endpoint, string $method, IntegrationTestResult $result): void
    {
        ApiRequestLog::create([
            'client_id' => $connection->client_id,
            'platform' => $connection->platform,
            'endpoint' => $endpoint,
            'method' => $method,
            'status_code' => $result->statusCode,
            'duration_ms' => $result->durationMs,
            'request_hash' => sha1($connection->id.'|'.$endpoint.'|'.now()->format('YmdHi')),
            'error_message' => $result->ok ? null : $result->message,
            'requested_at' => now(),
        ]);
    }
}
