@extends('layouts.admin')

@section('title', 'Analyses | Smart Dermatology')
@section('page_title', 'Skin Analyses')
@section('page_subtitle', 'Review uploaded images, predicted diseases, and the users behind them.')

@push('styles')
    <style>
        .analysis-thumb { width: 72px; height: 72px; object-fit: cover; border-radius: 16px; }
    </style>
@endpush

@section('content')
    <div class="card panel-card p-4">
        <form method="GET" action="{{ route('admin.analyses') }}" class="row g-3 mb-4">
            <div class="col-md-4">
                <label class="form-label fw-semibold">Search</label>
                <input type="text" name="search" class="form-control" value="{{ request('search') }}" placeholder="User, email, filename, disease">
            </div>
            <div class="col-md-3">
                <label class="form-label fw-semibold">Disease</label>
                <select name="disease" class="form-select">
                    <option value="">All diseases</option>
                    @foreach ($diseases as $disease)
                        <option value="{{ $disease }}" @selected(request('disease') === $disease)>{{ str_replace('_', ' ', $disease) }}</option>
                    @endforeach
                </select>
            </div>
            <div class="col-md-2">
                <label class="form-label fw-semibold">From</label>
                <input type="date" name="date_from" class="form-control" value="{{ request('date_from') }}">
            </div>
            <div class="col-md-2">
                <label class="form-label fw-semibold">To</label>
                <input type="date" name="date_to" class="form-control" value="{{ request('date_to') }}">
            </div>
            <div class="col-md-1 d-flex align-items-end">
                <button type="submit" class="btn btn-dark w-100">Go</button>
            </div>
            <div class="col-12 d-flex justify-content-end">
                <a href="{{ route('admin.analyses') }}" class="btn btn-outline-secondary rounded-pill">Reset Filters</a>
            </div>
        </form>

        <div class="table-responsive">
            <table class="table align-middle mb-0">
                <thead class="table-light">
                    <tr>
                        <th>Image</th>
                        <th>User</th>
                        <th>Filename</th>
                        <th>Disease</th>
                        <th>Confidence</th>
                        <th>Analyzed At</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse ($analyses as $analysis)
                        <tr>
                            <td><img src="{{ asset($analysis->image_path) }}" alt="Analyzed image" class="analysis-thumb"></td>
                            <td>
                                <div class="fw-semibold">{{ $analysis->user->name }}</div>
                                <div class="small text-muted">{{ $analysis->user->email }}</div>
                            </td>
                            <td>{{ $analysis->original_filename }}</td>
                            <td class="text-capitalize">{{ str_replace('_', ' ', $analysis->disease) }}</td>
                            <td>{{ number_format($analysis->confidence * 100, 2) }}%</td>
                            <td>{{ $analysis->created_at?->format('d M Y h:i A') }}</td>
                            <td class="text-end">
                                <form action="{{ route('admin.analyses.destroy', $analysis) }}" method="POST" onsubmit="return confirm('Delete analysis entry for {{ addslashes($analysis->original_filename) }}? This will also remove the saved image file.');">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="btn btn-sm btn-outline-danger rounded-pill">Delete</button>
                                </form>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="text-center text-muted py-5">No analyses have been recorded yet.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        @if ($analyses->hasPages())
            <div class="mt-4">
                {{ $analyses->links() }}
            </div>
        @endif
    </div>
@endsection
