@extends('admin.layouts.app')

@section('title', __('dashboard.view_role'))

@section('content')
    <x-admin.page-header :title="__('dashboard.view_role')" :breadcrumbs="[__('models.roles') => route('admin.roles.index'), __('dashboard.details') => '']" />

    <div class="row">
        <div class="col-lg-4">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">{{ __('dashboard.role_information') }}</h5>
                </div>
                <div class="card-body">
                    <div class="mb-3">
                        <label class="form-label text-muted">{{ __('dashboard.role_name') }}</label>
                        <h5 class="mb-0">{{ $role->name }}</h5>
                    </div>

                    <div class="mb-3">
                        <label class="form-label text-muted">{{ __('dashboard.assigned_admins') }}</label>
                        <p class="mb-0">
                            <span class="badge bg-info-subtle text-info fs-12">{{ $role->users->count() }} {{ __('models.admins') }}</span>
                        </p>
                    </div>

                    <div class="mb-3">
                        <label class="form-label text-muted">{{ __('dashboard.total_permissions') }}</label>
                        <p class="mb-0">
                            <span class="badge bg-primary-subtle text-primary fs-12">{{ $role->permissions->count() }} {{ __('dashboard.permissions') }}</span>
                        </p>
                    </div>

                    <div class="mb-3">
                        <label class="form-label text-muted">{{ __('models.created_at') }}</label>
                        <p class="mb-0">{{ $role->created_at->format('d M, Y H:i') }}</p>
                    </div>
                </div>
            </div>

            <div class="d-flex gap-2">
                <a href="{{ route('admin.roles.edit', $role->id) }}" class="btn btn-primary">
                    <i class="ri-edit-line align-bottom me-1"></i> {{ __('models.edit') }}
                </a>
                <a href="{{ route('admin.roles.index') }}" class="btn btn-light">
                    {{ __('models.back') }}
                </a>
            </div>
        </div>

        <div class="col-lg-8">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">{{ __('dashboard.permissions') }}</h5>
                </div>
                <div class="card-body">
                    @php
                        $groupedPermissions = $role->permissions->groupBy(fn($p) => explode('.', $p->name)[0]);
                    @endphp

                    @forelse($groupedPermissions as $group => $perms)
                        <div class="permission-group mb-4">
                            <div class="d-flex align-items-center mb-3">
                                <h6 class="mb-0 text-uppercase fw-bold">
                                    <i class="ri-folder-line me-1 text-primary"></i>
                                    {{ ucfirst(str_replace('_', ' ', $group)) }}
                                </h6>
                                <span class="badge bg-primary-subtle text-primary ms-2">{{ $perms->count() }}</span>
                            </div>
                            <div class="ps-4">
                                <div class="row g-2">
                                    @foreach ($perms as $permission)
                                        @php
                                            $action = explode('.', $permission->name)[1] ?? $permission->name;
                                            $actionIcon = match($action) {
                                                'list', 'index' => 'ri-list-check',
                                                'view', 'show' => 'ri-eye-line',
                                                'create', 'store' => 'ri-add-line',
                                                'edit', 'update' => 'ri-pencil-line',
                                                'delete', 'destroy' => 'ri-delete-bin-line',
                                                default => 'ri-checkbox-circle-line'
                                            };
                                            $actionColor = match($action) {
                                                'list', 'index' => 'info',
                                                'view', 'show' => 'primary',
                                                'create', 'store' => 'success',
                                                'edit', 'update' => 'warning',
                                                'delete', 'destroy' => 'danger',
                                                default => 'secondary'
                                            };
                                        @endphp
                                        <div class="col-md-4 col-sm-6">
                                            <div class="d-flex align-items-center p-2 rounded bg-light">
                                                <i class="{{ $actionIcon }} text-{{ $actionColor }} me-2 fs-16"></i>
                                                <span class="text-capitalize">{{ ucfirst(str_replace('_', ' ', $action)) }}</span>
                                            </div>
                                        </div>
                                    @endforeach
                                </div>
                            </div>
                        </div>
                        @if(!$loop->last)
                            <hr class="my-4">
                        @endif
                    @empty
                        <div class="alert alert-warning mb-0">
                            <i class="ri-alert-line me-2"></i>
                            {{ __('dashboard.no_permissions_assigned') }}
                        </div>
                    @endforelse
                </div>
            </div>
        </div>
    </div>
@endsection
