<?php

namespace App\Http\Controllers\SelfPaced\Public;

use App\Http\Controllers\Controller;
use App\Models\Organization;
use App\Services\SelfPacedCertificateService;
use Illuminate\Http\Request;

class VerificationController extends Controller
{
    public function __construct(protected SelfPacedCertificateService $certificateService)
    {
        // Fully public — no auth middleware
    }

    /**
     * Show the certificate verification page.
     * Requirement 11.3, 11.4, 11.5
     */
    public function index(Request $request)
    {
        $organization = Organization::first();
        $result       = null;
        $code         = $request->get('code');

        if ($code) {
            $result = $this->certificateService->verify($code);
        }

        return view('self-paced.public.verify', compact('organization', 'result', 'code'));
    }

    /**
     * AJAX verification lookup.
     * Requirement 11.4, 11.5
     */
    public function verify(Request $request)
    {
        $request->validate([
            'code' => 'required|string|max:100',
        ]);

        $result = $this->certificateService->verify($request->input('code'));

        return response()->json($result);
    }
}
