<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class verificationController extends Controller
{
    public function verifycert($refcode): JsonResponse
    {
        $record = DB::table('certified_members')
                ->where('cert_serial', $refcode)
                ->first();

        $record = DB::table('certified_members')
        ->join('tbl_courses', 'certified_members.courseCode', '=', 'tbl_courses.id')
        ->where('certified_members.cert_serial', $refcode)
        ->select('certified_members.*', 'tbl_courses.course_name as course_name')
        ->first();

        if ($record) {
            if ($record->status === 'valid') {
                return response()->json([
                    'status' => 'success',
                    'data' => $record
                ], 200);
            } else {
                return response()->json([
                    'status' => 'expired',
                    'message' => 'The certificate is expired.',
                    'current_status' => $record->status
                ], 403); // 403 Forbidden
            }
        }

        return response()->json([
            'status' => 'error',
            'message' => 'Refcode not found.'
        ], 404);
    }
}
