@extends('layouts.admin')

@section('admin-content')
    @php
        $sort = $sort ?? request('sort', 'active_domains_count');
        $direction = $direction ?? request('direction', 'desc');
        $toggleDirection = $direction === 'asc' ? 'desc' : 'asc';
    @endphp

    <div class="admin-page-header">
        <div class="admin-page-header-content">
            <div class="admin-page-title">
                <h1>License Domain Report</h1>
                <p class="admin-page-subtitle">Overview of domains attached to each license with sortable counts.</p>
            </div>
            <div class="admin-page-actions">
                <a href="{{ route('admin.licenses.index') }}" class="admin-btn admin-btn-secondary admin-btn-m">
                    <i class="fas fa-arrow-left me-2"></i>
                    Back to Licenses
                </a>
            </div>
        </div>
    </div>

    <div class="row g-4 mb-4">
        <div class="col-lg-3 col-md-6">
            <div class="card border-0 shadow-sm stats-card">
                <div class="card-body p-4">
                    <div class="d-flex align-items-center">
                        <div class="stats-icon bg-primary bg-opacity-10 rounded-circle p-3 me-3">
                            <i class="fas fa-key text-primary fs-4"></i>
                        </div>
                        <div class="flex-grow-1">
                            <p class="text-muted small mb-1 fw-medium">Total Licenses</p>
                            <h3 class="fw-bold mb-1 text-dark">{{ number_format($totals['total_licenses'] ?? 0) }}</h3>
                            <small class="text-primary fw-medium">All licenses in the system</small>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-lg-3 col-md-6">
            <div class="card border-0 shadow-sm stats-card">
                <div class="card-body p-4">
                    <div class="d-flex align-items-center">
                        <div class="stats-icon bg-success bg-opacity-10 rounded-circle p-3 me-3">
                            <i class="fas fa-globe text-success fs-4"></i>
                        </div>
                        <div class="flex-grow-1">
                            <p class="text-muted small mb-1 fw-medium">Total Domains</p>
                            <h3 class="fw-bold mb-1 text-dark">{{ number_format($totals['total_domains'] ?? 0) }}</h3>
                            <small class="text-success fw-medium">Registered across licenses</small>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-lg-3 col-md-6">
            <div class="card border-0 shadow-sm stats-card">
                <div class="card-body p-4">
                    <div class="d-flex align-items-center">
                        <div class="stats-icon bg-info bg-opacity-10 rounded-circle p-3 me-3">
                            <i class="fas fa-calculator text-info fs-4"></i>
                        </div>
                        <div class="flex-grow-1">
                            <p class="text-muted small mb-1 fw-medium">Avg Domains / License</p>
                            <h3 class="fw-bold mb-1 text-dark">{{ number_format($totals['avg_domains'] ?? 0, 2) }}</h3>
                            <small class="text-info fw-medium">Usage density</small>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-lg-3 col-md-6">
            <div class="card border-0 shadow-sm stats-card">
                <div class="card-body p-4">
                    <div class="d-flex align-items-center">
                        <div class="stats-icon bg-warning bg-opacity-10 rounded-circle p-3 me-3">
                            <i class="fas fa-chart-pie text-warning fs-4"></i>
                        </div>
                        <div class="flex-grow-1">
                            <p class="text-muted small mb-1 fw-medium">Domain Capacity</p>
                            <h3 class="fw-bold mb-1 text-dark">
                                {{ number_format($totals['usage_percent'] ?? 0, 1) }}%
                            </h3>
                            <small class="text-warning fw-medium">
                                {{ number_format($totals['total_domains'] ?? 0) }} /
                                {{ number_format($totals['max_capacity'] ?? 0) }} domains used
                            </small>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="card">
        {{-- <div class="card-header bg-light d-flex align-items-center justify-content-between">
            <div class="d-flex align-items-center">
                <i class="fas fa-list-alt me-3 text-primary"></i>
                <div>
                    <h5 class="card-title mb-0">License Domain Counts</h5>
                    <small class="text-muted">Sortable by total domains with pagination</small>
                </div>
            </div>
            <div class="d-flex gap-2">
                <form method="GET" action="{{ route('admin.licenses.report') }}" class="d-flex align-items-center">
                    <input type="hidden" name="sort" value="{{ $sort }}">
                    <label class="me-2 mb-0 small text-muted">Direction</label>
                    <select name="direction" class="form-select form-select-sm me-2" onchange="this.form.submit()">
                        <option value="desc" {{ $direction === 'desc' ? 'selected' : '' }}>High to Low</option>
                        <option value="asc" {{ $direction === 'asc' ? 'selected' : '' }}>Low to High</option>
                    </select>
                    <button type="submit" class="btn btn-outline-primary btn-sm">
                        <i class="fas fa-sort me-1"></i>Apply
                    </button>
                </form>
            </div>
        </div> --}}

        <div class="card-body p-0">
            @if ($licenses->count() > 0)
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead class="table-light">
                            <tr>
                                <th>License</th>
                                <th>User</th>
                                <th>System Type</th>
                                <th>Product</th>
                                <th class="text-center">
                                    <a href="{{ route('admin.licenses.report', array_merge(request()->except('page'), ['sort' => 'active_domains_count', 'direction' => $toggleDirection])) }}"
                                        class="text-decoration-none text-dark">
                                        Active Domains
                                        <i class="fas fa-sort-amount-{{ $direction === 'asc' ? 'up' : 'down' }} ms-1"></i>
                                    </a>
                                </th>
                                <th class="text-center">
                                    Total Domains
                                </th>
                                <th class="text-center">Max Domains Quota</th>
                                <th class="text-center">Status</th>
                                <th class="text-center">Created</th>
                                <th class="text-center">Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach ($licenses as $license)
                                <tr>
                                    <td>
                                        <div class="fw-semibold text-dark">{{ $license->license_key }}</div>
                                        <small class="text-muted">ID: {{ $license->id }}</small>
                                    </td>
                                    <td>
                                        <div class="fw-semibold text-dark">{{ $license->user->name ?? 'N/A' }}</div>
                                        <small class="text-muted">{{ $license->user->email ?? '' }}</small>
                                    </td>
                                    <td>{{ $license->product->device_type ?? 'N/A' }}</td>
                                    <td>{{ $license->product->name ?? 'N/A' }}</td>
                                    <td class="text-center">
                                        <span
                                            class="{{ $license->active_domains_count > $license->max_domains ? 'badge bg-danger' : 'badge bg-success' }}">
                                            {{ $license->active_domains_count ?? 0 }}
                                        </span>
                                    </td>
                                    <td class="text-center">
                                        <span class="badge bg-info }}">
                                            {{-- <span class="badge bg-info"> --}}
                                            {{ $license->domains_count ?? 0 }}
                                        </span>
                                    </td>
                                    <td class="text-center">
                                        <span class="badge bg-light text-dark">
                                            {{ $license->max_domains ?? '-' }}
                                        </span>
                                    </td>
                                    <td class="text-center">
                                        <span
                                            class="badge {{ $license->status === 'active' ? 'bg-success' : 'bg-secondary' }}">
                                            {{ ucfirst($license->status) }}
                                        </span>
                                    </td>
                                    <td class="text-center">
                                        <small class="text-muted">{{ $license->created_at?->format('Y-m-d') }}</small>
                                    </td>
                                    <td class="text-center">
                                        <a href="{{ route('admin.licenses.show', [$license, '#license_domain_section']) }}"
                                            class="btn btn-outline-info btn-sm">
                                            <i class="fas fa-eye me-1"></i>View
                                        </a>
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>

                <div class="card-footer">
                    <div class="d-flex justify-content-between align-items-center">
                        <div class="text-muted small">
                            Showing {{ $licenses->firstItem() }}-{{ $licenses->lastItem() }} of {{ $licenses->total() }}
                            records
                        </div>
                        {{ $licenses->links() }}
                    </div>
                </div>
            @else
                <div class="text-center py-5">
                    <div class="mb-4">
                        <i class="fas fa-list-alt text-muted empty-state-icon"></i>
                    </div>
                    <h4 class="text-muted">No licenses found</h4>
                    <p class="text-muted mb-4">Create a license to start tracking domain usage.</p>
                    <a href="{{ route('admin.licenses.create') }}" class="btn btn-primary btn-lg">
                        <i class="fas fa-plus me-2"></i>
                        Add License
                    </a>
                </div>
            @endif
        </div>
    </div>
@endsection
