<?php

namespace App\Http\Controllers\Web;

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

class BrowseCategoriesController extends Controller
{
    public function index(Request $request)
    {
        $contentTypes = \App\Models\ContentType::with(['rootCategories.children'])->ordered()->get();

        $categoriesByType = [];
        $categories = collect();
        foreach ($contentTypes as $ct) {
            $rootCats = $ct->rootCategories;
            foreach ($rootCats as $cat) {
                $cat->subcategories = $cat->children;
                $categories->push($cat);
            }
            $categoriesByType[$ct->slug] = $rootCats;
        }

        return view('browse-categories', [
            'contentTypes' => $contentTypes,
            'categoriesByType' => $categoriesByType,
            'categories' => $categories,
        ]);
    }
}
