use Gate;
use Carbon\Carbon;
use App\Models\Menu;
use App\Models\Recipe;
use Illuminate\Http\Request;
use Illuminate\Contracts\View\View;
class MenuController extends Controller
{
public function index(Request $request): View
{
abort_if(Gate::denies('menu_access'), 403);
if (! $request->has('week_start')) {
$date = Carbon::now()->startOfWeek();
$links = [
'prev' => Carbon::now()->subWeek()->startOfWeek()->format('Y-m-d'),
'next' => Carbon::now()->addWeek()->startOfWeek()->format('Y-m-d')
];
} else {
$date = Carbon::parse($request->query('week_start'))->startOfWeek();
$links = [
'prev' => Carbon::parse($request->query('week_start'))->subWeek()->startOfWeek()->format('Y-m-d'),
'next' => Carbon::parse($request->query('week_start'))->addWeek()->startOfWeek()->format('Y-m-d')
];
}
$weekDays = [
'Monday' => $date->format('Y-m-d'),
'Tuesday' => $date->addDay()->format('Y-m-d'),
'Wednesday' => $date->addDay()->format('Y-m-d'),
'Thursday' => $date->addDay()->format('Y-m-d'),
'Friday' => $date->addDay()->format('Y-m-d'),
'Saturday' => $date->addDay()->format('Y-m-d'),
'Sunday' => $date->addDay()->format('Y-m-d'),
];
$current = Menu::with('recipes', 'recipes.ingredients')->whereHas('recipes', function ($query) use ($date, $weekDays) {
$query->whereIn('date', $weekDays);
})->where('user_id', Auth::id())->first();
$recipeCount = Recipe::count();
return view('menus.index', compact('weekDays', 'current', 'links', 'recipeCount'));
}
//
}