<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class LinkController extends Controller
{
    public function product(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => ['required', 'integer'],
        ]);

        if ($validator->fails()) {
            return response()->rest($validator->messages()->get('*'), 400, 'Validator fail');
        }

        $product = DB::table('products')->where('id', $request->id)->pluck('slug', 'id');

        if ($product->isEmpty()) {
            return to_route('web.products.show', ['product' => 1]);
        }

        return to_route('web.products.show', ['product' => $product->first()]);
    }
}
