<?php

namespace App\Services;

use Illuminate\Support\Facades\Http;

class DVLAService
{
    protected $apiKey;

    public function __construct()
    {
        // Replace this with your actual API key
        $this->apiKey = 'fCj12mJEx62XItZYZ5HA52rgwjoVXkce5RABwjdV';
        // $this->apiKey = env('DVLA_API_KEY', 'YOUR_API_KEY');
    }

    /**
     * Perform a vehicle enquiry to the DVLA API
     * 
     * @param string $registrationNumber
     * @return array
     */
    public function enquireVehicle($registrationNumber)
    {
        // API URL
        $url = "https://driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1/vehicles";

        // Send the POST request using Laravel's HTTP client
        $response = Http::withHeaders([
            'x-api-key' => $this->apiKey,
            'Content-Type' => 'application/json',
        ])->post($url, [
            'registrationNumber' => $registrationNumber
        ]);

        // Check if the request was successful
        if ($response->successful()) {
            return $response->json();  // Return the response data as an array
        } else {
            return ['error' => 'API request failed', 'status' => $response->status(), 'body' => $response->body()];
        }
    }
}