@extends('layouts.app')
@section('title','API Integrations - NGBVD')

@section('content')
@include('integrations.partials.settings_style')
@php
    $summary = $summary ?? [];
    $clients = $summary['clients'] ?? 0;
    $activeClients = $summary['active_clients'] ?? 0;
    $success = $summary['success'] ?? 0;
    $failed = $summary['failed'] ?? 0;
    $pending = $summary['pending'] ?? 0;
    $recentLogs = $recentLogs ?? collect();
@endphp
<div class="ni-shell">
    <div class="ni-hero">
        <div>
            <span class="ni-kicker">Secure Data Exchange</span>
            <h2 class="ni-title">API Integrations</h2>
            <p class="ni-subtitle">Manage approved system-to-system data exchange with authorised platforms while maintaining auditability and survivor data protection.</p>
        </div>
        <div class="ni-actions">
            <a href="{{ url('/integrations/api-urls') }}" class="btn ngbvd-btn-soft"><i class="ti-link"></i> API URLs</a>
            <a href="{{ url('/integrations/api-clients') }}" class="btn ngbvd-btn-primary"><i class="ti-key"></i> API Clients</a>
        </div>
    </div>

    <div class="ni-grid">
        <div class="ni-stat"><span class="ni-stat-icon"><i class="ti-id-badge"></i></span><div><small>API Clients</small><strong>{{ number_format($clients) }}</strong><span class="text-muted">{{ number_format($activeClients) }} active</span></div></div>
        <div class="ni-stat"><span class="ni-stat-icon"><i class="ti-check-box"></i></span><div><small>Successful Shares</small><strong>{{ number_format($success) }}</strong><span class="text-muted">Completed</span></div></div>
        <div class="ni-stat"><span class="ni-stat-icon"><i class="ti-alert"></i></span><div><small>Failed Shares</small><strong>{{ number_format($failed) }}</strong><span class="text-muted">Requires review</span></div></div>
        <div class="ni-stat"><span class="ni-stat-icon"><i class="ti-timer"></i></span><div><small>Pending</small><strong>{{ number_format($pending) }}</strong><span class="text-muted">Awaiting response</span></div></div>
    </div>

    <div class="ni-layout-wide">
        <div class="ni-card">
            <div class="ni-card-body">
                <div class="ni-heading">
                    <div>
                        <h4>Integration Workspace</h4>
                        <p>Use these work areas to configure API access, review endpoints and track all data-sharing attempts.</p>
                    </div>
                    <span class="ni-heading-icon"><i class="ti-plug"></i></span>
                </div>
                <div class="ni-nav-cards">
                    <a class="ni-nav-card" href="{{ url('/integrations/api-urls') }}">
                        <span class="ni-nav-icon"><i class="ti-link"></i></span>
                        <div><h5>API URLs</h5><p>View available v2 endpoints, authentication requirements and testing guidance.</p></div>
                    </a>
                    <a class="ni-nav-card" href="{{ url('/integrations/api-clients') }}">
                        <span class="ni-nav-icon"><i class="ti-key"></i></span>
                        <div><h5>API Clients</h5><p>Create and manage credentials for approved external systems.</p></div>
                    </a>
                    <a class="ni-nav-card" href="{{ url('/integrations/logs') }}">
                        <span class="ni-nav-icon"><i class="ti-clipboard"></i></span>
                        <div><h5>Sharing Logs</h5><p>Audit outbound and inbound API activity, failures, retries and responses.</p></div>
                    </a>
                    <a class="ni-nav-card" href="{{ url('/audit/logs') }}">
                        <span class="ni-nav-icon"><i class="ti-shield"></i></span>
                        <div><h5>System Audit</h5><p>Review user activity and permission-controlled access events.</p></div>
                    </a>
                </div>
            </div>
        </div>

        <div class="ni-card ni-panel">
            <div class="ni-card-body">
                <div class="ni-heading">
                    <div>
                        <h4>Data Protection Notes</h4>
                        <p>Operational guidance for approved sharing.</p>
                    </div>
                    <span class="ni-heading-icon"><i class="ti-shield"></i></span>
                </div>
                <div class="ni-info mb-3"><i class="ti-lock"></i><span>Use separate API clients for partner systems. Secrets are hashed and should be copied only at creation.</span></div>
                <div class="ni-info mb-3"><i class="ti-eye"></i><span>Integration logs keep request and response previews encoded to reduce exposure of sensitive information.</span></div>
            </div>
        </div>
    </div>

    <div class="ni-card mt-3">
        <div class="ni-card-body">
            <div class="ni-heading">
                <div><h4>Recent Sharing Activity</h4><p>Latest API exchange attempts recorded by the system.</p></div>
                <span class="ni-heading-icon"><i class="ti-list"></i></span>
            </div>
            <div class="ni-table-wrap">
                <table class="table ni-table" data-datatable="false">
                    <thead><tr><th>Date</th><th>System</th><th>Direction</th><th>Case</th><th>Status</th><th>HTTP</th><th>Log</th></tr></thead>
                    <tbody>
                    @forelse($recentLogs as $log)
                        @php $status=strtolower($log->status ?? 'pending'); $cls=$status==='success'?'ni-success':(in_array($status,['failed','error'])?'ni-danger':'ni-warning'); @endphp
                        <tr>
                            <td>{{ optional($log->created_at)->format('d M Y H:i') ?: 'N/A' }}</td>
                            <td><strong>{{ $log->system_name ?: $log->integration_name ?: 'External System' }}</strong></td>
                            <td><span class="ni-badge ni-blue">{{ ucfirst($log->direction ?: 'outbound') }}</span></td>
                            <td>{{ $log->case_reference ?: 'N/A' }}</td>
                            <td><span class="ni-badge {{ $cls }}">{{ ucfirst($status) }}</span></td>
                            <td>{{ $log->response_code ?: '-' }}</td>
                            <td>@if(!empty($log->uuid))<a class="btn btn-sm ngbvd-btn-soft" href="{{ url('/integrations/logs/'.$log->uuid) }}"><i class="ti-eye"></i> View</a>@else<span class="text-muted">N/A</span>@endif</td>
                        </tr>
                    @empty
                        <tr><td colspan="7"><div class="ni-empty"><i class="ti-clipboard"></i><strong>No sharing activity yet</strong><span>API sharing attempts will appear here once records are exchanged.</span></div></td></tr>
                    @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
@endsection
