<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\View;
use App\Actions\App\ShowMenu;
use App\Models\ConfigApp;
use App\Models\Siswa;
use App\Traits\ApiResponseTrait;
use Exception;

class PageMenuController extends Controller
{
    use ApiResponseTrait;

    // protected $baseAction;

    public function __construct()
    {
        // Tentukan model yang digunakan
        // $this->baseAction = new ShowMenu();
    }


    public function getMenuPage(Request $request, ShowMenu $action)
    {
        // return $this->baseAction->page();
        try {
            $item = $action->page($request->all());
            return $this->apiResponse($item)
                ->message('Data berhasil ditemukan')
                ->send();
        } catch (Exception $e) {
            return $this->handleException($e, 'Gagal memproses menu');
        }
    }

    public function dashboard()
    {
        if (!Auth::check()) {
            return redirect()->intended('/login');
        }

        $dataApp = ConfigApp::all()
            ->groupBy('config_tipe')
            ->mapWithKeys(function ($items, $type) {
                return [
                    $type => $items->pluck('config_value', 'config_name')
                ];
            })->toArray();

        
        $user = Auth::user();
        $user->sub_title = $user->email;
        if($user->primary_role_id === 3){
            $siswa = Siswa::find($user->biodata_id);
            $user->sub_title = $siswa->nisn;
        }
        View::share('biodata', $user);

        $navHead = [];
        View::share('nav_head', $navHead);
        return view('admin.app',$dataApp);
    }
}
