<?php

namespace App\Modules\ServiceBooking\Http\Resources;

use App\Facades\Settings;
use App\Http\Resources\ProcessingLogResource;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Str;

class ViewServiceBooking extends JsonResource
{
    public static $wrap = '';

    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    #[\Override]
    public function toArray(Request $request): array
    {
        $this->resource->load([
            'location',
            'packages',
            'deliveryOption',
            'processingLogs',
        ]);
        $this->resource->data = collect($this->resource->data ?? [])
            ->keyBy(fn ($value, $name) => Str::title(str_replace('_', ' ', $name)));
        $this->resource->preview_image = $this->previewImage();
        $resource = parent::toArray($request);
        $resource['processing_logs'] = ProcessingLogResource::collection($this->resource->processingLogs);
        return $resource;
    }

    public function previewImage(): string
    {
        return 'https://cdn.imagin.studio/getImage?' . http_build_query([
                'customer' => Settings::get('imagin-placeholders-client-key') ?: config('services.imagin-studio.key'),
                'make' => $this->resource->vehicle_data['make'] ?? null,
                'modelFamily' => $this->resource->vehicle_data['model'] ?? null,
                'paintdescription' => $this->resource->vehicle_data['colour'] ?? null,
                'modelYear' => $this->resource->vehicle_data['model_year'] ?? null,
                'aspectRatio' => '16:9',
                'zoomLevel' => 50,
            ]);
    }
}
