<?php

namespace App\Http\Controllers\V3;

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

class PermissionManagementController extends Controller
{
    use ApiResponse;
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request): JsonResponse
    {
        return response()->json(DataTableFacade::run(
            Permission::query(),
            $request,
            allowedFilters: ['*'],
            allowedSortings: ['*'])
        );
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreRequest $request): JsonResponse
    {
        Permission::create([
            'name' => $request->name,
            'guard_name' => 'web',
            'name_fa' => $request->name_fa,
            'description' => $request->description,
            'type' => $request->type,
            'type_fa' => $request->type_fa,
            'need_province' => $request->need_province,
            'need_edare_shahri' => $request->need_edare_shahri,
        ]);

        return $this->successResponse();
    }

    /**
     * Display the specified resource.
     */
    public function show(Permission $permission): JsonResponse
    {
        return $this->successResponse($permission);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateRequest $request, Permission $permission): JsonResponse
    {
        $permission->update([
            'name' => $request->name,
            'name_fa' => $request->name_fa,
            'description' => $request->description,
            'type' => $request->type,
            'type_fa' => $request->type_fa,
            'need_province' => $request->need_province,
            'need_edare_shahri' => $request->need_edare_shahri,
        ]);

        return $this->successResponse();
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Permission $permission): JsonResponse
    {
        $permission->delete();
        return $this->successResponse();
    }
}
