<?php

namespace App\Http\Controllers\Api;

use App\Enums\ResponseStatus;
use App\Http\Controllers\Controller;
use App\Http\Requests\ContactEnquiryRequest;
use App\Models\ApiRequest;
use App\Models\Website;
use Illuminate\Http\JsonResponse;

class ContactEnquiryController extends Controller
{
    public function __invoke(ContactEnquiryRequest $request): JsonResponse
    {
        $website = $request->user();
        $registrationNumber = strtoupper(str_replace(' ', '', $request->validated('registration_number')));

        ApiRequest::create([
            'website_id' => $website->id,
            'registration_number' => $registrationNumber,
            'ip_address' => $request->ip(),
            'contact_data' => $request->validated('contact_data'),
            'response_status' => ResponseStatus::ContactSubmitted,
            'metadata' => [
                'user_agent' => $request->userAgent(),
                'referer' => $request->header('referer'),
            ],
            'created_at' => now(),
        ]);

        return response()->json(['success' => true]);
    }
}
