<x-app-layout title="Edit device">
    <div class="mx-auto max-w-lg space-y-4">
        <div>
            <a href="{{ route('care.devices.index') }}" class="text-sm text-slate-500 hover:text-slate-800">← Devices</a>
            <h1 class="mt-2 text-xl font-semibold text-slate-900">{{ $device->name }}</h1>
            <p class="mt-1 text-sm text-slate-500">{{ $connectionHint }}</p>
        </div>

        @if ($plainToken)
            <div class="rounded-2xl border border-amber-200 bg-amber-50 px-4 py-3 text-sm text-amber-950">
                <p class="font-medium">Device token (copy now — shown once)</p>
                <code class="mt-2 block break-all rounded-lg bg-white px-3 py-2 font-mono text-xs text-slate-800">{{ $plainToken }}</code>
                <p class="mt-2 text-xs text-amber-800">Send as <code class="font-mono">X-Care-Device-Token</code> or <code class="font-mono">Authorization: Bearer …</code> from the Care Device Agent.</p>
            </div>
        @elseif ($device->hasToken())
            <div class="rounded-2xl border border-slate-200 bg-slate-50 px-4 py-3 text-sm text-slate-700">
                <p>A device token is active (hash stored). Regenerate to rotate; the previous token stops working immediately.</p>
            </div>
        @endif

        <form method="POST" action="{{ route('care.devices.update', $device) }}" class="space-y-4 rounded-2xl border border-slate-200 bg-white p-6">
            @csrf @method('PUT')
            <div>
                <label class="block text-sm font-medium text-slate-700">Name</label>
                <input type="text" name="name" value="{{ old('name', $device->name) }}" required class="mt-1 w-full rounded-lg border-slate-300 text-sm">
            </div>
            <div>
                <label class="block text-sm font-medium text-slate-700">Type</label>
                <select name="type" required class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                    @foreach ($deviceTypes as $value => $label)
                        @php $needsPro = in_array($value, $agentTypes, true); @endphp
                        <option value="{{ $value }}" @selected(old('type', $device->type) === $value)
                                @disabled($needsPro && ! $hasAgentDevices && $device->type !== $value)>
                            {{ $label }}
                        </option>
                    @endforeach
                </select>
            </div>
            <div>
                <label class="block text-sm font-medium text-slate-700">Connection mode</label>
                <select name="connection_mode" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                    @foreach ($connectionModes as $value => $label)
                        <option value="{{ $value }}" @selected(old('connection_mode', $device->connection_mode) === $value)>{{ $label }}</option>
                    @endforeach
                </select>
            </div>
            <div>
                <label class="block text-sm font-medium text-slate-700">Status</label>
                <select name="status" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                    @foreach ($deviceStatuses as $value => $label)
                        <option value="{{ $value }}" @selected(old('status', $device->status) === $value)>{{ $label }}</option>
                    @endforeach
                </select>
            </div>
            <div>
                <label class="block text-sm font-medium text-slate-700">Branch</label>
                <select name="branch_id" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                    <option value="">All branches</option>
                    @foreach ($branches as $branch)
                        <option value="{{ $branch->id }}" @selected(old('branch_id', $device->branch_id) == $branch->id)>{{ $branch->name }}</option>
                    @endforeach
                </select>
            </div>
            <div class="flex gap-2">
                <button type="submit" class="btn-primary">Save</button>
                <a href="{{ route('care.devices.index') }}" class="rounded-lg border border-slate-200 px-4 py-2 text-sm text-slate-600">Cancel</a>
            </div>
        </form>

        <div class="flex flex-wrap gap-2">
            <form method="POST" action="{{ route('care.devices.token.regenerate', $device) }}">
                @csrf
                <button type="submit" class="rounded-lg border border-slate-200 bg-white px-4 py-2 text-sm text-slate-700 hover:bg-slate-50">
                    {{ $device->hasToken() ? 'Regenerate token' : 'Issue agent token' }}
                </button>
            </form>
            @if ($device->hasToken())
                <form method="POST" action="{{ route('care.devices.token.revoke', $device) }}">
                    @csrf @method('DELETE')
                    <button type="submit" class="rounded-lg border border-rose-200 bg-white px-4 py-2 text-sm text-rose-700 hover:bg-rose-50">Revoke token</button>
                </form>
            @endif
            <form method="POST" action="{{ route('care.devices.destroy', $device) }}" onsubmit="return confirm('Remove this device?')">
                @csrf @method('DELETE')
                <button type="submit" class="rounded-lg border border-rose-200 bg-white px-4 py-2 text-sm text-rose-700 hover:bg-rose-50">Delete device</button>
            </form>
        </div>
    </div>
</x-app-layout>
