<?php

namespace App\Http\Controllers\V3;

use App\Facades\DataTable\DataTableFacade;
use App\Http\Controllers\Controller;
use App\Http\Requests\V3\LogList\StoreRequest;
use App\Http\Requests\V3\LogList\UpdateRequest;
use App\Http\Traits\ApiResponse;
use App\Models\LogList;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class LogListManagementController extends Controller
{
    use ApiResponse;

    public function index(Request $request): JsonResponse
    {
        return response()->json(DataTableFacade::run(
            LogList::query(),
            $request,
            allowedFilters: ['*'],
            allowedSortings: ['*']));
    }

    public function list(): JsonResponse
    {
        return $this->successResponse(LogList::all('description', 'log_unique_code'));
    }

    public function store(StoreRequest $request): JsonResponse
    {
        LogList::query()->create([
            'description' => $request->description,
            'log_unique_code' => $request->log_unique_code,
            'action_type' => $request->action_type,
        ]);

        return $this->successResponse();
    }

    public function show(LogList $logList): JsonResponse
    {
        return $this->successResponse($logList);
    }

    public function update(UpdateRequest $request, LogList $logList): JsonResponse
    {
        $logList->update([
            'description' => $request->description,
            'log_unique_code' => $request->log_unique_code,
            'action_type' => $request->action_type,
        ]);
        return $this->successResponse();
    }

    public function destroy(LogList $logList): JsonResponse
    {
        $logList->delete();
        return $this->successResponse();
    }
}
