@extends('layouts.admin')

@section('admin-content')

    <div class="admin-page-header">
        <div class="admin-page-header-content d-flex justify-content-between align-items-center">
            <div>
                <h1>{{ __('Package Versions') }}</h1>
                <p class="admin-page-subtitle">Manage all uploaded package versions</p>
            </div>
            <div>
                <a href="{{ route('admin.packageversions.create') }}" class="btn btn-primary">
                    <i class="fas fa-plus me-1"></i> Add New Version
                </a>
            </div>
        </div>
    </div>

    <!-- Filters -->
    <div class="card mb-4 border-0 shadow-sm">
        <div class="card-body">
            <form method="GET" action="" class="row g-3">
                <div class="col-md-4">
                    <input type="text" name="search" class="form-control" placeholder="Search by version / tech stack"
                        value="{{ request('search') }}">
                </div>

                <div class="col-md-3">
                    <select name="tech_stack" class="form-select">
                        <option value="">Filter by Tech Stack</option>
                        @foreach ($techStacks as $stack)
                            <option value="{{ $stack }}" {{ request('tech_stack') == $stack ? 'selected' : '' }}>
                                {{ $stack->name }}
                            </option>
                        @endforeach
                    </select>
                </div>

                <div class="col-md-3">
                    <select name="status" class="form-select">
                        <option value="">Filter by Status</option>
                        <option value="draft" {{ request('status') == 'draft' ? 'selected' : '' }}>Draft</option>
                        <option value="published" {{ request('status') == 'published' ? 'selected' : '' }}>Published
                        </option>
                        <option value="archived" {{ request('status') == 'archived' ? 'selected' : '' }}>Archived</option>
                    </select>
                </div>

                <div class="col-md-2">
                    <button class="btn btn-primary w-100">
                        <i class="fas fa-search me-1"></i> Apply
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- Table -->
    <div class="card">
        <div class="card-header bg-light">
            <h5 class="card-title mb-0">
                <i class="fas fa-archive me-2 text-primary"></i> Package Versions
            </h5>
        </div>

        <div class="card-body p-0">
            @if ($versions->count() > 0)
                <div class="table-responsive">
                    <table class="table table-hover mb-0 align-middle">
                        <thead class="table-light">
                            <tr>
                                <th class="text-center" style="width: 5%;">ID</th>
                                <th style="width: 15%;">Tech Stack</th>
                                <th style="width: 10%;">Version</th>
                                <th style="width: 10%;">File Size</th>
                                <th style="width: 15%;">Status</th>
                                <th style="width: 15%;">Created At</th>
                                <th class="text-center" style="width: 30%;">Actions</th>
                            </tr>
                        </thead>

                        <tbody>
                            @foreach ($versions as $v)
                                <tr>
                                    <td class="text-center">{{ $v->id }}</td>
                                    <td>
                                        <span class="badge bg-secondary">{{ $v->techStack->name }}</span>
                                    </td>
                                    <td>
                                        <span class="badge bg-info">{{ $v->version }}</span>
                                    </td>
                                    <td>
                                        @php $sizeKB = strlen($v->file_base64) / 1024; @endphp
                                        {{ number_format($sizeKB, 2) }} KB
                                    </td>

                                    <td>
                                        <div class="d-flex align-items-center">
                                            <!-- Status Badge -->
                                            <span
                                                class="badge 
            @if ($v->status == 'draft') bg-success
            @elseif($v->status == 'published') bg-danger
            @elseif($v->status == 'archived') bg-secondary @endif
        ">
                                                {{ ucfirst($v->status) }}
                                            </span>

                                            <button class="btn btn-outline-primary btn-md ms-2 edit-status-btn"
                                                data-id="{{ $v->id }}" data-status="{{ $v->status }}"
                                                title="Change Status">
                                                <i class="fas fa-edit"></i>
                                            </button>
                                        </div>
                                    </td>


                                    <td>{{ $v->created_at->format('Y-m-d H:i') }}</td>

                                    <td class="text-center">
                                        <div class="d-inline-flex align-items-center gap-1">
                                            <a href="{{ route('admin.packageversions.show', $v->id) }}"
                                                class="btn btn-sm btn-outline-primary" title="View">
                                                <i class="fas fa-eye"></i>
                                            </a>

                                            <a href="{{ route('admin.packageversions.edit', $v->id) }}"
                                                class="btn btn-sm btn-outline-warning" title="Edit">
                                                <i class="fas fa-edit"></i>
                                            </a>

                                            <a href="{{ route('admin.packageversions.downloadExtracted', $v->id) }}"
                                                class="btn btn-sm btn-outline-success" title="Download Extracted Files">
                                                <i class="fas fa-download"></i>
                                            </a>

                                            <form action="{{ route('admin.packageversions.destroy', $v->id) }}"
                                                method="POST" class="d-inline"
                                                onsubmit="return confirm('Delete this version?')">
                                                @csrf
                                                @method('DELETE')
                                                <button class="btn btn-sm btn-outline-danger" title="Delete">
                                                    <i class="fas fa-trash"></i>
                                                </button>
                                            </form>
                                        </div>
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>

                <div class="card-footer">
                    <div class="d-flex justify-content-center">
                        {{ $versions->links() }}
                    </div>
                </div>
            @else
                <div class="text-center py-5">
                    <i class="fas fa-box-open text-muted empty-state-icon"></i>
                    <h4 class="text-muted mt-3">No Versions Found</h4>
                    <p class="text-muted">Upload package versions to get started</p>
                </div>
            @endif
        </div>
    </div>

    <!-- Status Update Modal -->
    <div class="modal fade" id="statusModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog">
            <form method="POST" id="statusUpdateForm">
                @csrf
                <div class="modal-content">
                    <div class="modal-header btn-primary">
                        <h5 class="modal-title text-white">Update Status</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                    </div>

                    <div class="modal-body">
                        <label class="form-label fw-bold">Select Status</label>
                        <select name="status" class="form-select" id="statusSelect">
                            <option value="draft">Draft</option>
                            <option value="published">Published</option>
                            <option value="archived">Archived</option>
                        </select>
                    </div>

                    <div class="modal-footer">
                        <button type="button" class="btn btn-light" data-bs-dismiss="modal">Cancel</button>
                        <button class="btn btn-primary">Update</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

@endsection

@push('scripts')
    <script>
        $(document).on('click', '.edit-status-btn', function() {
            let id = $(this).data('id');
            let status = $(this).data('status');
            $('#statusUpdateForm').attr('action', '/admin/package-versions/' + id + '/toggle-status');
            $('#statusSelect').val(status);
            $('#statusModal').modal('show');
        });
    </script>
@endpush
