<?php

namespace App\Http\Controllers\Dashboard;

use Illuminate\Http\Request;

class BreadcrumbController extends Controller
{
    public function index(Request $request)
    {
        $path = $request->path();
        $pathSnippets = array_filter(explode('/', $path));
        $root = empty($pathSnippets);

        // Simulated pageRoutes (replace with your actual routes)
        $pageRoutes = [
            ['path' => '/my-profile', 'name' => 'Home'],
            ['path' => '/my-profile/users', 'name' => 'Users', 'params' => true],
        ];

        $extraBreadcrumbItems = [];
        foreach ($pathSnippets as $index => $snippet) {
            $url = '/' . implode('/', array_slice($pathSnippets, 0, $index + 1));
            $route = collect($pageRoutes)->firstWhere('path', $url);
            if ($route) {
                $paramsId = end($pathSnippets);
                $extraBreadcrumbItems[] = [
                    'url' => $route['params'] ? "$url/$paramsId" : $url,
                    'name' => $route['name'],
                ];
            }
        }

        return view('dashboard.breadcrumbs', compact('root', 'extraBreadcrumbItems'));
    }
}