[Back]
<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class QrAdminController extends Controller
{
    public function index()
    {
        // Fetch all checkpoints (you can filter by floor/block if you want)
        $checkpoints = DB::table('checkpoints')
            ->orderBy('floor_id')->orderBy('name')
            ->get(['id','slug','name','floor_id']);

        // Build entries: absolute URL + SVG QR
        $entries = $checkpoints->map(function ($cp) {
            // URL that scanning this sticker should open:
            // - sets current location via /qr/{slug}
            // - s=1 asks the map page to auto-speak the next step
            $url = route('nav.qr', ['slug' => $cp->slug, 's' => 1]);

            // SVG is best for printing; high ECC and small margin
            $svg = QrCode::format('svg')
                ->size(400)          // render size (will be scaled by CSS)
                ->margin(1)
                ->errorCorrection('H')
                ->generate($url);

            // For convenience, map floor name (optional)
            $floorName = DB::table('floors')->where('id', $cp->floor_id)->value('name') ?? 'Floor';

            return [
                'name'      => $cp->name,
                'slug'      => $cp->slug,
                'floor'     => $floorName,
                'url'       => $url,
                'qr_svg'    => $svg,
            ];
        });

        return view('admin.qr.index', ['entries' => $entries]);
    }
}