<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Participant;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;

class ParticipantController extends Controller
{
    public static function createFromToken(Request $request, string $id)
    {
        if ($request->input('password') !== env('ADMIN_PASSWORD')) {
            Log::info(env('ADMIN_PASSWORD'));
            return response()->json(['message' => 'Unauthorized: Wrong password'], 401);
        }

        if(!Participant::where('user_id', $id)->exists()) {
            $participant = Participant::create([
                'user_id' => $id,
                'token' => uniqid()
            ]);

            return $participant->token;
        }
        else {
            $participant = Participant::where('user_id', $id)->first();
            return $participant->token;
        }
    }
}
