<?php

    namespace App\Http\Controllers\Api;

    use App\Http\Requests\Api\ProductsIndex;
    use App\Http\Resources\EventCollection;
    use App\Models\Product;
    use Illuminate\Http\Request;
    use App\Http\Controllers\Controller;
    use App\Facades\Services\BackendUserService;
    use App\Http\Requests\Api\BaseShow;
    use App\Http\Resources\ProductCollection;
    use App\Http\Resources\ProductResource;

    class ProductController extends Controller
    {
        /**
         * Display a listing of the resource.
         * @param  ProductsIndex  $request
         * @return \Illuminate\Http\Response
         */
        public function index(ProductsIndex $request)
        {
            $data = $request->validated();
            $model = $data['model'];
            $component = $data['component'];
            $tags = $data['tags'];
            $user = BackendUserService::getLoggedUser();
            $query = Product::full()
                ->select('*');
            if (!$user) {
                $query->where('status', 'published');
            }
            $query->with([
                'productGroup' => function ($query) use ($user) {
                    $query->full();
                    if (!$user) {
                        $query->where('status', 'published');
                    }
                },
                'otherImages',
                'highlightImage',
                'components' => function ($query) use ($user) {
                    $query->full()->select('*');
                    if (!$user) {
                        $query->where('status', 'published');
                    }
                },
                
             ])
                ->orderBy('website_objects.position', 'asc');
            if ($model !== 0) {
                $query->whereHas('productGroup', function ($query) use ($model) {
                    $query->where('product_group_id', $model);
                });
            }

            if ($component !== 0) {
                $query->whereHas('components', function ($query) use ($component) {
                    $query->where('component_id', $component);
                });
            }

            if (count($tags) > 0) {
                foreach ($tags as $tag) {
                    $query->whereHas('productTags', function ($query) use ($tag) {
                        $query->where('tag_id', $tag);
                    });
                }
            }

            $products = $query->paginate(12);
            return new ProductCollection($products);
        }

        /**
         * Display the specified resource.
         *
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        public function show(BaseShow $request)
        {
            // Retrieve the validated input data...
            $data = $request->validated();
            $user = BackendUserService::getLoggedUser();
            $product = Product::full()
                ->select('*')
                ->with([
                    'productGroup',
                    'otherImages',
                    'highlightImage',
                    'productBlocks' => function ($query) use ($user) {
                        $query->full()
                            ->select('*')
                            ->orderBy('block_product.position', 'asc')
                            ->with([
                                'blockListBoxItems' => function ($query) use ($user) {
                                    $query->full()->select('*')->orderBy('block_list_box_items.position', 'asc');
                                }
                            ]);
                    },
                    'otherVersions',
                    'relatedProducts' => function ($query) use ($user) {
                        $query->full()->select('*')->with('highlightImage');
                        if (!$user) {
                            $query->where('status', 'published');
                        }
                    }
                ])
                ->where('lang_slug', $data['slug'])
                ->first();
            return new ProductResource($product, true);
        }

        /**
         * Update the specified resource in storage.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        public function update(Request $request, $id)
        {
            //
        }

        /**
         * Remove the specified resource from storage.
         *
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        public function destroy($id)
        {
            //
        }
    }
