[Back]
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class NavController extends Controller
{
    // Existing: start navigating to a destination (kept as you have)
    public function to(string $block, string $slug)
    {
        $dest = DB::table('checkpoints')->where('slug', $slug)->first();
        if (!$dest) {
            return redirect()->route('map.show', 'floor1')
                ->with('status', "Destination checkpoint '{$slug}' not found. Add it via /map/coords/{floor}.");
        }

        session([
            'nav.dest_slug' => $slug,
            'nav.dest_name' => $dest->name,
        ]);

        if (!session()->has('nav.current_id')) {
            $fallback = DB::table('checkpoints')
                ->where('floor_id', $dest->floor_id)
                ->where('type', 'entrance')
                ->orderBy('id')->first()
                ?? DB::table('checkpoints')->where('slug', 'entrance-f1')->first()
                ?? $dest;
            session(['nav.current_id' => $fallback->id]);
        }

        $current = DB::table('checkpoints')->find(session('nav.current_id'));
        $floorSlug = DB::table('floors')->where('id', $current->floor_id)->value('slug') ?: 'floor1';

        // s=1 asks the map page to auto-speak next instruction
        return redirect()->route('map.show', [$floorSlug, 's' => 1])
            ->with('status', "Navigation started to {$dest->name}");
    }

    // NEW: handle QR that sets CURRENT (and optional ?to=dest slug)
    // public function qr(string $slug, Request $request)
    // {
    //     $cp = DB::table('checkpoints')->where('slug', $slug)->first();
    //     if (!$cp) abort(404, 'Checkpoint not found');

    //     session(['nav.current_id' => $cp->id]);

    //     // Optional ?to=slug to set destination with the same scan
    //     if ($request->filled('to')) {
    //         $dest = DB::table('checkpoints')->where('slug', $request->query('to'))->first();
    //         if ($dest) {
    //             session(['nav.dest_slug' => $dest->slug, 'nav.dest_name' => $dest->name]);
    //         }
    //     }

    //     $floorSlug = DB::table('floors')->where('id', $cp->floor_id)->value('slug') ?? 'floor1';
    //     return redirect()->route('map.show', [$floorSlug, 's' => 1])
    //         ->with('status', "Location updated: {$cp->name}");
    // }




    // NEW: QR that encodes both current and destination in path
    // public function qrTo(string $slug, string $dest)
    // {
    //     $cp = DB::table('checkpoints')->where('slug', $slug)->first();
    //     if (!$cp) abort(404, 'Checkpoint not found');

    //     session(['nav.current_id' => $cp->id]);

    //     $destCp = DB::table('checkpoints')->where('slug', $dest)->first();
    //     if ($destCp) session(['nav.dest_slug' => $destCp->slug, 'nav.dest_name' => $destCp->name]);

    //     $floorSlug = DB::table('floors')->where('id', $cp->floor_id)->value('slug') ?? 'floor1';
    //     return redirect()->route('map.show', [$floorSlug, 's' => 1])
    //         ->with('status', $destCp ? "Location set: {$cp->name}. Navigating to {$destCp->name}" : "Location set: {$cp->name}");
    // }


    // When the user scans a QR at a checkpoint
    public function qr(string $checkpointSlug)
    {
        $cp = \DB::table('checkpoints')->where('slug', $checkpointSlug)->first();
        abort_unless($cp, 404, 'Checkpoint not found');

        session(['nav.current_id' => $cp->id]);

        $floorSlug = \DB::table('floors')->where('id', $cp->floor_id)->value('slug') ?? 'floor1';
        return redirect()->route('map.show', [$floorSlug, 's' => 1])
            ->with('status', "Location updated: {$cp->name}");
    }

    // QR that sets current + destination
    public function qrTo(string $slug, string $dest)
    {
        $cp = \DB::table('checkpoints')->where('slug', $slug)->first();
        abort_unless($cp, 404, 'Checkpoint not found');
        session(['nav.current_id' => $cp->id]);

        $destCp = \DB::table('checkpoints')->where('slug', $dest)->first();
        if ($destCp) session(['nav.dest_slug' => $destCp->slug, 'nav.dest_name' => $destCp->name]);

        $floorSlug = \DB::table('floors')->where('id', $cp->floor_id)->value('slug') ?? 'floor1';
        return redirect()->route('map.show', [$floorSlug, 's' => 1])
            ->with('status', $destCp ? "Location set: {$cp->name}. Navigating to {$destCp->name}" : "Location set: {$cp->name}");
    }





    // NEW: in-app QR scanner page
    public function scan()
    {
        return view('nav.scan');
    }

    public function clear()
    {
        session()->forget(['nav.dest_slug', 'nav.dest_name', 'nav.current_id']);
        return redirect()->route('map.show', 'floor1')->with('status', 'Navigation cleared');
    }

    public function set(Request $request)
    {
        $slug = $request->input('slug');
        $cp = \DB::table('checkpoints')->where('slug', $slug)->first();
        if (!$cp) {
            return back()->with('status', "Checkpoint '{$slug}' not found.");
        }
        session(['nav.current_id' => $cp->id]);

        $floorSlug = \DB::table('floors')->where('id', $cp->floor_id)->value('slug') ?? 'floor1';
        return redirect()->route('map.show', $floorSlug)->with('status', "Location set: {$cp->name}");
    }
}