<?php

namespace App\Http\Controllers\Api\V1\RoleManagement;

use App\Http\Controllers\Controller;
use App\Http\Resources\Api\V1\PermissionResource;
use App\Services\PermissionService;
use App\Services\ResponseService;
use Illuminate\Http\JsonResponse;

class PermissionController extends Controller
{
    protected PermissionService $permissionService;
    protected ResponseService $responseService;

    public function __construct(PermissionService $permissionService, ResponseService $responseService)
    {
        $this->permissionService = $permissionService;
        $this->responseService = $responseService;
    }

    public function index(): JsonResponse
    {
        try {
            $permissions = $this->permissionService->getAllPermissions();

            return $this->responseService->success(
                PermissionResource::collection($permissions),
                'Permissions retrieved successfully'
            );
        } catch (\Exception $e) {
            return $this->responseService->serverError('Failed to retrieve permissions', $e->getMessage());
        }
    }

    public function byModule(): JsonResponse
    {
        try {
            $permissions = $this->permissionService->getPermissionsByModule();

            return $this->responseService->success(
                $permissions,
                'Permissions by module retrieved successfully'
            );
        } catch (\Exception $e) {
            return $this->responseService->serverError('Failed to retrieve permissions', $e->getMessage());
        }
    }
}
