<?php

namespace App\Http\Controllers;

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

class OpenGameController extends Controller
{
    public function launch(Request $request): RedirectResponse|Response
    {
        $ticket = trim((string) $request->query('gameUrl', ''));

        if ($ticket === '') {
            return response('');
        }

        $play = DB::table('tb_play')->where('tc', $ticket)->first();

        if ($play === null) {
            return response('');
        }

        DB::table('tb_user')
            ->where('intUser', (string) $play->user)
            ->where('status', 'Pending')
            ->update(['status' => 'Success']);

        return redirect()->away((string) $play->url);
    }
}
