<?php

namespace App\Http\Controllers;

use App\Http\Requests\Menu\StoreMenuRequest;
use App\Http\Requests\Menu\UpdateMenuRequest;
use App\Models\Group;
use App\Models\Menu;
use App\Models\MenuType;
use App\Services\NavigationService;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;

class MenuController extends Controller
{
    public function __construct(
        private readonly NavigationService $navigationService,
    ) {}

    /**
     * Daftar menu (FR-USR-05), dikelompokkan per section.
     */
    public function index(): Response
    {
        $menus = Menu::with(['groups', 'menuType', 'parent'])
            ->orderBy('section')
            ->orderBy('sort_order')
            ->get();

        return Inertia::render('menus/Index', [
            'menus' => $menus,
            'menuTypes' => MenuType::orderBy('type')->get(['id', 'type']),
            'groups' => Group::orderBy('name')->get(['id', 'name']),
        ]);
    }

    public function create(): Response
    {
        return Inertia::render('menus/Create', [
            'menuTypes' => MenuType::orderBy('type')->get(['id', 'type']),
            'groups' => Group::orderBy('name')->get(['id', 'name']),
            'parents' => Menu::whereNull('parent_id')->orderBy('label')->get(['id', 'label']),
        ]);
    }

    public function store(StoreMenuRequest $request): RedirectResponse
    {
        $data = $request->validated();

        $menu = Menu::create([
            'label' => $data['label'],
            'route' => $data['route'] ?? null,
            'link' => $data['link'] ?? null,
            'icon' => $data['icon'] ?? null,
            'section' => $data['section'] ?? 'Utama',
            'menu_type_id' => $data['menu_type_id'],
            'parent_id' => $data['parent_id'] ?? null,
            'sort_order' => $data['sort_order'] ?? 0,
            'status' => $data['status'] ?? true,
        ]);

        $menu->groups()->sync($data['groups'] ?? []);
        $this->navigationService->clearCache();

        Inertia::flash('toast', ['type' => 'success', 'message' => __('Menu berhasil dibuat.')]);

        return to_route('menus.index');
    }

    public function edit(Menu $menu): Response
    {
        return Inertia::render('menus/Edit', [
            'menu' => $menu->load('groups'),
            'menuTypes' => MenuType::orderBy('type')->get(['id', 'type']),
            'groups' => Group::orderBy('name')->get(['id', 'name']),
            'parents' => Menu::whereNull('parent_id')->where('id', '!=', $menu->id)->orderBy('label')->get(['id', 'label']),
        ]);
    }

    public function update(UpdateMenuRequest $request, Menu $menu): RedirectResponse
    {
        $data = $request->validated();

        $menu->update([
            'label' => $data['label'],
            'route' => $data['route'] ?? null,
            'link' => $data['link'] ?? null,
            'icon' => $data['icon'] ?? null,
            'section' => $data['section'] ?? 'Utama',
            'menu_type_id' => $data['menu_type_id'],
            'parent_id' => $data['parent_id'] ?? null,
            'sort_order' => $data['sort_order'] ?? 0,
            'status' => $data['status'] ?? true,
        ]);

        $menu->groups()->sync($data['groups'] ?? []);
        $this->navigationService->clearCache();

        Inertia::flash('toast', ['type' => 'success', 'message' => __('Menu berhasil diperbarui.')]);

        return to_route('menus.index');
    }

    public function destroy(Menu $menu): RedirectResponse
    {
        $menu->delete();
        $this->navigationService->clearCache();

        Inertia::flash('toast', ['type' => 'success', 'message' => __('Menu berhasil dihapus.')]);

        return to_route('menus.index');
    }
}
