<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\Role\StoreRoleRequest;
use App\Http\Requests\Role\UpdateRoleRequest;
use App\Http\Resources\RoleResource;
use App\Services\RoleService;
use Illuminate\Http\Request;

class RoleController extends Controller
{
    protected $roleService;

    public function __construct(RoleService $roleService)
    {
        $this->roleService = $roleService;
    }

    public function index(Request $request)
    {
        $result = $this->roleService->index($request->all(), $request->user('admin'));

        if (isset($result['code']) && $result['code'] !== 0) {
            return response()->json($result);
        }

        $result['data']['items'] = RoleResource::collection($result['data']['items']);

        return response()->json($result);
    }

    public function tree(Request $request)
    {
        $result = $this->roleService->tree($request->user('admin'));

        return response()->json($result);
    }

    public function store(StoreRoleRequest $request)
    {
        $result = $this->roleService->store($request->validated(), $request->user());

        if (isset($result['code']) && $result['code'] !== 0) {
            return response()->json($result);
        }

        $result['data'] = new RoleResource($result['data']);

        return response()->json($result);
    }

    public function show($id)
    {
        $result = $this->roleService->show($id);

        if (isset($result['code']) && $result['code'] !== 0) {
            return response()->json($result);
        }

        $result['data'] = new RoleResource($result['data']);

        return response()->json($result);
    }

    public function update(UpdateRoleRequest $request, $id)
    {
        $result = $this->roleService->update($id, $request->validated());

        if (isset($result['code']) && $result['code'] !== 0) {
            return response()->json($result);
        }

        $result['data'] = new RoleResource($result['data']);

        return response()->json($result);
    }

    public function destroy($id)
    {
        $result = $this->roleService->destroy($id);

        return response()->json($result);
    }

    public function getPermissions(Request $request, $id)
    {
        $result = $this->roleService->getPermissions($id, $request->user('admin'));

        return response()->json($result);
    }

    public function updatePermissions(Request $request, $id)
    {
        $request->validate([
            'permission_ids' => 'required|array',
            'permission_ids.*' => 'exists:admin_permissions,id',
        ]);

        $result = $this->roleService->updatePermissions($id, $request->permission_ids, $request->user('admin'));

        return response()->json($result);
    }

    public function getMenus(Request $request, $id)
    {
        $result = $this->roleService->getMenus($id, $request->user('admin'));

        return response()->json($result);
    }

    public function updateMenus(Request $request, $id)
    {
        $request->validate([
            'menu_ids' => 'required|array',
            'menu_ids.*' => 'exists:admin_menus,id',
        ]);

        $result = $this->roleService->updateMenus($id, $request->menu_ids, $request->user('admin'));

        return response()->json($result);
    }

    public function all(Request $request)
    {
        $result = $this->roleService->all($request->user('admin'));

        return response()->json($result);
    }

    public function updateStatus(Request $request, $id)
    {
        $request->validate([
            'status' => 'required|in:0,1',
        ]);

        $result = $this->roleService->updateStatus($id, $request->status);

        return response()->json($result);
    }
}
