<?php

namespace App\Http\Controllers\Site\Ajax;

use Illuminate\Http\Request;
use App\Models\Admin\Taxonomy;
use App\Http\Controllers\Controller;
use App\Http\Resources\Api\v1\Taxonomy\TaxonomyResourceCollection;

class TaxonomyFilterController extends Controller
{
    public function filter(Request $request)
    {
        $mainCategoryId = $request->input('main_category_id');
        $productCategoryId = $request->input('product_category_id');

        $productCategories = Taxonomy::where('parent_id', $mainCategoryId)->get();

        $figures = [];
        $impaCodes = [];
        if ($productCategoryId) {
            $taxonomyResourceCollection = new TaxonomyResourceCollection(Taxonomy::where('parent_id', $mainCategoryId)->get(), $mainCategoryId, $productCategoryId);
            $figures = $taxonomyResourceCollection->getDikkanFigures();
            $impaCodes = $taxonomyResourceCollection->getImpaCodes();
        }

        return response()->json([
            'productCategories' => view('site.product.partials.product-categories', compact('productCategories'))->render(),
            'figures' => view('site.product.partials.figures', compact('figures'))->render(),
            'impaCodes' => view('site.product.partials.impa-codes', compact('impaCodes'))->render(),
        ]);
    }
}
