<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Models\ExtraMenu;
use Illuminate\Http\Request;

class MenuController extends Controller
{

    public function index()
    {
        $extraMenus = ExtraMenu::select('id', 'menu_name', 'menu_link')->get();
        return sendSuccessResponse($extraMenus->map(function ($extraMenu) {
            return [
                'id' => $extraMenu->id,
                'menu_name' => $extraMenu->menu_name,
                'menu_link' => $extraMenu->menu_link
            ];
        }));
    }

    public function extraMenus()
    {
        return view('Backend.ExtraMenus.extra-menus', [
            'extraMenus' => ExtraMenu::select('id', 'menu_name', 'menu_link')->get()
        ]);
    }

    public function store(Request $request)
    {
        $request->validate([
            'menu_name' => 'required|string|max:255',
            'menu_link' => 'required|string|max:255|url',
        ]);

        ExtraMenu::create($request->all());
        return back()
            ->with('success', 'Menu added successfully')
            ->with('toast', true);
    }

    public function update(Request $request, $id)
    {
        $request->validate([
            'menu_name' => 'required|string|max:255',
            'menu_link' => 'required|string|max:255|url',
        ]);

        ExtraMenu::find($id)->update($request->all());
        return back()
            ->with('success', 'Menu updated successfully')
            ->with('toast', true);
    }

    public function delete(Request $request)
    {
        ExtraMenu::find($request->menu_id)->delete();
        return back()
            ->with('success', 'Menu deleted successfully')
            ->with('toast', true);
    }
}
