<?php

namespace App\Http\Controllers\Api\V1\Product\Barcode;

use App\Http\Controllers\Api\V1\Product\Barcode\Requests\ProductBarcodeSearchIndexRequest;
use App\Http\Controllers\Controller;
use App\Models\Ecommerce\Product\Product\Product;
use Illuminate\Http\JsonResponse;

class ProductBarcodeSearchController extends Controller
{
    /**
     * Search product via barcode
     */
    public function index(ProductBarcodeSearchIndexRequest $request): JsonResponse
    {
        $product = Product::where('barcode', $request->barcode)->with('media')->first();

        return response()->rest([
            'id' => $product->id,
            'name' => $product->name,
            'stock' => $product->stock,
            'price_amount' => $product->price_amount,
            'image' => [
                'thumbnail' => $product->thumbnail(),
                'images_400x400' => $product->getMedia('uploads')->map(fn ($media) => $media->getUrl('thumb400x400')),
                'images_720x720' => $product->getMedia('uploads')->map(fn ($media) => $media->getUrl('thumb720x720')),
                'images_800x800' => $product->getMedia('uploads')->map(fn ($media) => $media->getUrl('thumb800x800')),
                'images_1200x1200' => $product->getMedia('uploads')->map(fn ($media) => $media->getUrl('thumb1200x1200')),
            ],
        ]);
    }
}
