<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\NavigationMenu;
use App\Services\AuditService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class MenuController extends Controller
{
    public function __construct(protected AuditService $auditService) {}

    public function index(Request $request): View
    {
        $location = $request->input('location', 'admin');
        if (! in_array($location, ['admin', 'portal'], true)) {
            $location = 'admin';
        }

        $menus = NavigationMenu::query()
            ->where('location', $location)
            ->orderBy('sort_order')
            ->orderBy('label')
            ->get();

        return view('admin.menus.index', compact('menus', 'location'));
    }

    public function store(Request $request): RedirectResponse
    {
        $data = $this->validated($request);
        $menu = NavigationMenu::query()->create($data);
        NavigationMenu::flushCache();
        $this->auditService->log('created', 'navigation', NavigationMenu::class, $menu->id, null, $menu->toArray());

        return back()->with('success', 'Menu item “'.$menu->label.'” created.');
    }

    public function update(Request $request, NavigationMenu $menu): RedirectResponse
    {
        $old = $menu->toArray();
        $menu->update($this->validated($request));
        NavigationMenu::flushCache();
        $this->auditService->log('updated', 'navigation', NavigationMenu::class, $menu->id, $old, $menu->fresh()->toArray());

        return back()->with('success', 'Menu item “'.$menu->label.'” updated.');
    }

    public function destroy(NavigationMenu $menu): RedirectResponse
    {
        $old = $menu->toArray();
        $label = $menu->label;
        $menu->delete();
        NavigationMenu::flushCache();
        $this->auditService->log('deleted', 'navigation', NavigationMenu::class, $old['id'] ?? null, $old, null);

        return back()->with('success', 'Menu item “'.$label.'” removed.');
    }

    protected function validated(Request $request): array
    {
        $data = $request->validate([
            'location' => ['required', 'in:admin,portal'],
            'label' => ['required', 'string', 'max:120'],
            'route_name' => ['nullable', 'string', 'max:160'],
            'url' => ['nullable', 'string', 'max:255'],
            'icon' => ['nullable', 'string', 'max:64'],
            'sort_order' => ['nullable', 'integer', 'min:0', 'max:9999'],
            'badge_key' => ['nullable', 'string', 'max:64'],
        ]);

        $data['is_visible'] = $request->boolean('is_visible');
        $data['open_in_new_tab'] = $request->boolean('open_in_new_tab');
        $data['sort_order'] = (int) ($data['sort_order'] ?? 0);
        $data['route_name'] = ($data['route_name'] ?? null) ?: null;
        $data['url'] = ($data['url'] ?? null) ?: null;
        $data['icon'] = ($data['icon'] ?? null) ?: null;
        $data['badge_key'] = ($data['badge_key'] ?? null) ?: null;

        return $data;
    }
}
