<?php

namespace App\Http\Controllers;

use App\Enums\DbStatusFieldEnum;
use App\Models\systemParameters;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Validator;

class DashboardController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(): View
    {
        $customContent = systemParameters::where('name', 'dashboardCustomContent')->firstOrCreate([
            'name' => 'dashboardCustomContent',
            'name_display' => 'Dashboard Custom Content',
            'data_type' => 'text',
            'status' => DbStatusFieldEnum::active(),
        ]
        )->select(['name', 'id', 'stored_data']);
        // dd($customContent->toArray());
        // $customContentData=$customContent->select(['name','id','stored_data'])->get()->toArray();
        $customContentData = $customContent->first()->toArray();

        return view('dashboard')->with(['customContentData' => $customContentData]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request) {}

    /**
     * Display the specified resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function show(int $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function edit(int $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, int $id): JsonResponse
    {
        $validated = Validator::make($request->all(), [
            'stored_data' => 'string|nullable|max:65000',
        ]);
        $newItem = systemParameters::findOrFail($id);
        $newItem->fill($validated->validated());

        $newItem->save();

        return response()->json($newItem->toArray());
    }

    /**
     * Remove the specified resource from storage.
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy(int $id)
    {
        //
    }
}
