<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\PermissionManagement\StoreRequest;
use App\Http\Requests\Admin\PermissionManagement\UpdateRequest;
use App\Http\Resources\Admin\PermissionResource;
use app\Http\Traits\ApiResponse;
use App\Models\Permission;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class PermissionManagementController extends Controller
{
    use ApiResponse;

    public function index(): JsonResponse
    {
        return $this->successResponse(Permission::all());
    }

    public function list(): JsonResponse
    {
        return $this->successResponse(PermissionResource::collection(Permission::all()));
    }

    public function store(StoreRequest $request): JsonResponse
    {
        Permission::query()->create([
            'name' => $request->name,
            'name_fa' => $request->name_fa,
            'guard_name' => 'web'
        ]);
        return $this->successResponse();
    }

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

    public function update(UpdateRequest $request, Permission $permission): JsonResponse
    {
        $permission->update([
            'name' => $request->name,
            'name_fa' => $request->name_fa,
            'guard_name' => 'web',
        ]);
        return $this->successResponse();
    }

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