@extends('layouts.app')

@section('title', 'Users')

@section('content')
    <x-page-header
        title="Users"
        :subtitle="trans_choice(':count account|:count accounts', $users->total(), ['count' => $users->total()])">
        <a href="{{ route('users.create') }}" class="btn btn-primary">
            <i class="bi bi-plus-lg" aria-hidden="true"></i>Add User
        </a>
    </x-page-header>

    {{--
        A generated password is shown exactly once, here, immediately after the
        action that produced it. It is never stored in readable form and cannot
        be retrieved again — a fresh reset is the only way to get another.
    --}}
    @if (session('generated_password'))
        <div class="alert alert-warning ams-no-print">
            <i class="bi bi-key-fill" aria-hidden="true"></i>
            <div class="flex-grow-1">
                <div class="fw-semibold">Temporary password for {{ session('generated_password_for') }}</div>
                <p class="mb-2">
                    Shown once and not recoverable. Pass it to the account holder
                    directly — they must choose their own password at first sign-in.
                </p>
                <span class="ams-secret">{{ session('generated_password') }}</span>
            </div>
        </div>
    @endif

    <x-table.wrapper>
        <x-slot:toolbar>
            <x-filter-bar
                :action="route('users.index')"
                :active="(bool) array_filter($filters)">

                <x-filter.search :value="$filters['search']" placeholder="Search name or email address…" />

                <x-filter.select
                    name="status"
                    label="Status"
                    placeholder="Any status"
                    :selected="$filters['status']"
                    :options="['active' => 'Active', 'disabled' => 'Disabled']" />

                <x-filter.select
                    name="role"
                    label="Access level"
                    placeholder="Any access level"
                    :selected="$filters['role']"
                    :options="['master' => 'Master User', 'user' => 'User']" />
            </x-filter-bar>
        </x-slot:toolbar>

        <x-slot:head>
            <tr>
                <th scope="col">Name</th>
                <th scope="col">Access</th>
                <th scope="col">Status</th>
                <th scope="col">Last Sign-In</th>
                <th scope="col" class="text-end">Actions</th>
            </tr>
        </x-slot:head>

        @forelse ($users as $listedUser)
            <tr>
                <td>
                    <div class="d-flex align-items-center gap-2">
                        <span class="ams-avatar" aria-hidden="true">{{ $listedUser->initials() }}</span>
                        <div class="min-width-0">
                            <div class="ams-cell-primary ams-truncate">
                                {{ $listedUser->name }}
                                @if ($listedUser->is(auth()->user()))
                                    <x-badge color="light">You</x-badge>
                                @endif
                            </div>
                            <div class="ams-cell-secondary ams-truncate">{{ $listedUser->email }}</div>
                        </div>
                    </div>
                </td>

                <td>
                    @if ($listedUser->is_master)
                        <x-badge color="primary" icon="shield-lock">Master User</x-badge>
                    @else
                        <x-badge color="secondary">User</x-badge>
                    @endif
                </td>

                <td>
                    <div class="d-flex flex-wrap gap-1">
                        @if ($listedUser->is_active)
                            <x-badge color="success" dot>Active</x-badge>
                        @else
                            <x-badge color="secondary" dot>Disabled</x-badge>
                        @endif

                        @if ($listedUser->must_change_password)
                            <x-badge color="warning"
                                     icon="key"
                                     data-bs-toggle="tooltip"
                                     title="Has a temporary password that has not been changed">
                                Password pending
                            </x-badge>
                        @endif
                    </div>
                </td>

                <td class="text-nowrap ams-cell-secondary">
                    @if ($listedUser->last_login_at)
                        {{ $listedUser->last_login_at->format(setting('datetime_format', 'd M Y, H:i')) }}
                    @else
                        Never
                    @endif
                </td>

                <td class="text-end text-nowrap">
                    <x-table.actions>
                        <a href="{{ route('users.edit', $listedUser) }}"
                           class="btn btn-sm btn-icon"
                           data-bs-toggle="tooltip"
                           title="Edit">
                            <i class="bi bi-pencil" aria-hidden="true"></i>
                            <span class="visually-hidden">Edit {{ $listedUser->name }}</span>
                        </a>

                        @can('toggleStatus', $listedUser)
                            <form method="POST"
                                  action="{{ route('users.toggle-status', $listedUser) }}"
                                  data-ams-confirm="{{ $listedUser->is_active
                                      ? 'Disable the account for ' . $listedUser->name . '? They will be signed out immediately.'
                                      : 'Enable the account for ' . $listedUser->name . '?' }}"
                                  data-ams-confirm-title="{{ $listedUser->is_active ? 'Disable account' : 'Enable account' }}"
                                  data-ams-confirm-action="{{ $listedUser->is_active ? 'Disable' : 'Enable' }}">
                                @csrf
                                @method('PATCH')
                                <button type="submit"
                                        class="btn btn-sm btn-icon"
                                        data-bs-toggle="tooltip"
                                        title="{{ $listedUser->is_active ? 'Disable' : 'Enable' }}">
                                    <i class="bi bi-{{ $listedUser->is_active ? 'person-slash' : 'person-check' }}"
                                       aria-hidden="true"></i>
                                    <span class="visually-hidden">
                                        {{ $listedUser->is_active ? 'Disable' : 'Enable' }} {{ $listedUser->name }}
                                    </span>
                                </button>
                            </form>
                        @endcan

                        @can('resetPassword', $listedUser)
                            <form method="POST"
                                  action="{{ route('users.reset-password', $listedUser) }}"
                                  data-ams-confirm="Reset the password for {{ $listedUser->name }}? Their current password will stop working immediately."
                                  data-ams-confirm-title="Reset password"
                                  data-ams-confirm-action="Reset password">
                                @csrf
                                @method('PATCH')
                                <button type="submit"
                                        class="btn btn-sm btn-icon"
                                        data-bs-toggle="tooltip"
                                        title="Reset password">
                                    <i class="bi bi-key" aria-hidden="true"></i>
                                    <span class="visually-hidden">Reset password for {{ $listedUser->name }}</span>
                                </button>
                            </form>
                        @endcan

                        @can('delete', $listedUser)
                            <form method="POST"
                                  action="{{ route('users.destroy', $listedUser) }}"
                                  data-ams-confirm="Delete the account for {{ $listedUser->name }}? They will be signed out and will no longer be able to sign in."
                                  data-ams-confirm-title="Delete account"
                                  data-ams-confirm-action="Delete account">
                                @csrf
                                @method('DELETE')
                                <button type="submit"
                                        class="btn btn-sm btn-icon btn-outline-danger"
                                        data-bs-toggle="tooltip"
                                        title="Delete">
                                    <i class="bi bi-trash3" aria-hidden="true"></i>
                                    <span class="visually-hidden">Delete {{ $listedUser->name }}</span>
                                </button>
                            </form>
                        @endcan
                    </x-table.actions>
                </td>
            </tr>
        @empty
            <tr>
                <td colspan="5" class="p-0">
                    <x-table.empty-state
                        icon="people"
                        title="No users match these filters"
                        :message="array_filter($filters)
                            ? 'Try clearing the filters to see every account.'
                            : 'Add the first colleague who needs access to Aumni AMS.'">
                        @if (array_filter($filters))
                            <a href="{{ route('users.index') }}" class="btn btn-outline-secondary">
                                <i class="bi bi-x-lg" aria-hidden="true"></i>Clear filters
                            </a>
                        @else
                            <a href="{{ route('users.create') }}" class="btn btn-primary">
                                <i class="bi bi-plus-lg" aria-hidden="true"></i>Add User
                            </a>
                        @endif
                    </x-table.empty-state>
                </td>
            </tr>
        @endforelse

        @if ($users->hasPages())
            <x-slot:footer>
                <x-table.count :paginator="$users" noun="account" />
                {{ $users->links() }}
            </x-slot:footer>
        @endif
    </x-table.wrapper>
@endsection
