@extends('layouts.app')

@section('content')
<div class="text-center my-4">
    <h4 class="fw-bold">Emergency Activation Core</h4>
    <p class="text-muted small">Pressing the button below instantly notifies regional dispatch units and security agencies with your real-time telemetry data.</p>
    
    <!-- Heavy Action Interactive Emergency Activation Element -->
    <div class="my-5 d-flex justify-content-center">
        <button type="button" class="btn btn-danger rounded-circle shadow-lg d-flex flex-column align-items-center justify-content-center" style="width: 180px; height: 180px;" onclick="triggerPanicSignal()">
            <i class="fa-solid fa-hand-fist fa-3x mb-2 animate-pulse"></i>
            <span class="fw-bold tracking-wide">BROADCAST PANIC</span>
        </button>
    </div>
</div>

<div class="card border-0 shadow-sm rounded-3 mt-4">
    <div class="card-header bg-white border-0 pt-3">
        <h6 class="fw-bold mb-0 text-primary"><i class="fa-solid fa-newspaper me-2"></i>Recent Security Broadcast Updates</h6>
    </div>
    <div class="card-body">
        <div class="list-group list-group-flush">
            @forelse($news as $item)
                <div class="list-group-item px-0 py-3">
                    <div class="d-flex w-100 justify-content-between align-items-center">
                        <h6 class="mb-1 fw-bold text-dark">{{ $item->title }}</h6>
                        <span class="badge bg-{{ $item->severity === 'critical' ? 'danger' : ($item->severity === 'medium' ? 'warning' : 'info') }} text-uppercase small">{{ $item->severity }}</span>
                    </div>
                    <p class="mb-1 text-secondary small">{{ $item->content }}</p>
                </div>
            @empty
                <p class="text-muted text-center py-3 small">No alerts or security broadcast updates reported today.</p>
            @endforelse
        </div>
    </div>
</div>

<script>
function triggerPanicSignal() {
    // Determine if execution context is inside Native Android Client Wrapper Bridge
    if (typeof AndroidBridge !== "undefined") {
        AndroidBridge.triggerNativePanic(loggedUserId);
    } else {
        // Standard Web Browser Geolocation fallback logic execution
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position) {
                fetch('/api/external/signal', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': '{{ csrf_token() }}' },
                    body: JSON.stringify({
                        user_id: loggedUserId,
                        latitude: position.coords.latitude,
                        longitude: position.coords.longitude
                    })
                })
                .then(res => res.json())
                .then(data => alert('Web Alert Dispatched successfully via fallback layer.'))
                .catch(err => alert('Signal transmission failure over HTTP fallback.'));
            });
        } else {
            alert('Your active environment profile does not support geographic locating.');
        }
    }
}
</script>
@endsection
