<div class="user-api-keys">
    <div class="row">
        {{-- MAIN --}}
        <div class="col-12 col-md-9">

            {{-- CREATE TOKEN --}}
            @can('manageApiKeys', $user)
            <div class="card mb-3">
                <div class="card-body">
                    <h4 class="card-title mb-3">
                        Create API Key
                    </h4>

                    <div class="mb-3">
                        <label class="form-label">
                            Token Name
                        </label>
                        <input class="form-control"
                               wire:model.defer="tokenName"
                               placeholder="e.g. Reporting Integration">

                        @error('tokenName')
                        <div class="text-danger small">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="mb-4">
                        <label class="form-label">
                            API Scopes
                            <span class="text-muted small">(access control)</span>
                        </label>

                        <div class="alert alert-info small">
                            These scopes control what this API key can access.
                            They do <strong>not</strong> affect the user’s normal permissions.
                        </div>

                        @foreach($scopes as $group => $scopesInGroup)
                            <div class="card mb-2 border">
                                <div class="card-header bg-light fw-semibold">
                                    {{ $group }}
                                </div>

                                <div class="card-body">
                                    @foreach($scopesInGroup as $scope)
                                        <div class="form-check mb-2">
                                            <input class="form-check-input"
                                                   type="checkbox"
                                                   wire:model.defer="selectedScopes"
                                                   value="{{ $scope->name }}"
                                                   id="scope_{{ $scope->id }}">

                                            <label class="form-check-label"
                                                   for="scope_{{ $scope->id }}">
                                                <strong>{{ $scope->label }}</strong>

                                                <div class="text-muted small">
                                                    {{ $scope->description }}
                                                </div>

                                                <code class="small text-muted">
                                                    {{ $scope->name }}
                                                </code>
                                            </label>
                                        </div>
                                    @endforeach
                                </div>
                            </div>
                        @endforeach
                    </div>

                    <div class="d-flex justify-content-end">
                        <button class="btn btn-success"
                                wire:click="createToken">
                            Create API Key
                        </button>
                    </div>

                    {{-- ONE-TIME TOKEN DISPLAY --}}
                    @if($newToken)
                        <div class="alert alert-warning mt-4">
                            <strong>Copy this API key now.</strong>
                            It will not be shown again.

                            <div class="input-group mt-2">
                                <input id="new-api-token"
                                       class="form-control"
                                       value="{{ $newToken }}"
                                       readonly>

                                <button class="btn btn-outline-secondary"
                                        onclick="navigator.clipboard.writeText(
                                            document.getElementById('new-api-token').value
                                        )">
                                    Copy
                                </button>
                            </div>
                        </div>
                    @endif
                </div>
            </div>
            @endcan

            {{-- EXISTING TOKENS --}}
            <div class="card">
                <div class="card-body">
                    <h4 class="card-title mb-3">
                        Active API Keys
                    </h4>

                    <div class="table-responsive">
                        <table class="table table-striped align-middle">
                            <thead class="table-light">
                            <tr>
                                <th>Name</th>
                                <th>Scopes</th>
                                <th>Last Used</th>
                                <th class="text-end"></th>
                            </tr>
                            </thead>

                            <tbody>
                            @forelse($tokens as $token)
                                <tr>
                                    <td>
                                        <strong>{{ $token->name }}</strong>
                                    </td>

                                    <td>
                                        @foreach($token->abilities as $ability)
                                            <span class="badge bg-soft-secondary text-dark">
                                                {{ $ability }}
                                            </span>
                                        @endforeach
                                    </td>

                                    <td class="text-muted">
                                        {{ $token->last_used_at?->diffForHumans() ?? 'Never' }}
                                    </td>

                                    <td class="text-end">
                                        @can('manageApiKeys', $user)
                                        <button class="btn btn-sm btn-outline-danger"
                                                wire:click="revokeToken({{ $token->id }})"
                                                onclick="return confirm('Revoke this API key?')">
                                            Revoke
                                        </button>
                                        @endcan
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="4"
                                        class="text-center text-muted py-4">
                                        No API keys created.
                                    </td>
                                </tr>
                            @endforelse
                            </tbody>
                        </table>
                    </div>

                </div>
            </div>

        </div>

        {{-- SIDEBAR --}}
        <div class="col-12 col-md-3">
            <div class="card">
                <div class="card-body">
                    <h4 class="card-title">
                        Security Notes
                    </h4>

                    <hr>

                    <p class="text-muted small mb-0">
                        API keys allow programmatic access.
                        Use narrow scopes and revoke keys that
                        are no longer required.
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>
