<?php

namespace App\Http\Controllers;

use App\Http\Api\fiver;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class GameController extends Controller
{
    /**
     * ============================
     * HALAMAN SLOT PRAGMATIC
     * ============================
     */
    public function slotsPragmatic()
    {
        $gamelist = DB::table('fiver_games2')
            ->where('game_provider', 'PRAGMATIC')
            ->where('game_category', 'slot')
            ->where('status', 1)
            ->orderBy('game_name')
            ->get();

        return view('slots.pragmatic-play', compact('gamelist'));
    }

    /**
     * ============================
     * LAUNCH GAME (AJAX)
     * ============================
     * POST /game/launch
     */
   public function connect_games(Request $request)
{
    $request->validate([
        'game_code' => 'required|string'
    ]);

    $game = DB::table('fiver_games2')
        ->where('game_code', $request->game_code)
        ->where('status', 1)
        ->first();

    if (!$game) {
        return response()->json([
            'status' => false,
            'message' => 'Game tidak ditemukan'
        ], 404);
    }

    $user = auth()->user();
    if (!$user) {
        return response()->json([
            'status' => false,
            'message' => 'Silakan login terlebih dahulu'
        ], 401);
    }

    if (isset($user->status) && (int)$user->status !== 1) {
        return response()->json([
            'status' => false,
            'message' => 'Akun Anda dikunci dari akses game'
        ], 403);
    }

    if (empty($user->extplayer)) {
        $user->extplayer = 'U' . $user->id;
        $user->save();
    }

    try {
        $fiver = new fiver();

        // create user (duplicate = OK)
        $fiver->createUser($user->extplayer);

       $provider = $game->game_provider;

// khusus Pragmatic Live Casino
if ($provider === 'PRAGMATICLIVE') {
    $provider = 'PP_LIVE_PRO';
}

$response = $fiver->openGame(
    $user->extplayer,
    $game->game_code,
    $provider
);

        if (
            isset($response['status']) &&
            (int)$response['status'] === 1 &&
            !empty($response['launch_url'])
        ) {
            return response()->json([
                'status' => true,
                'url' => $response['launch_url']
            ]);
        }

        Log::error('FIVER OPEN GAME FAILED', [
            'user_id' => $user->id,
            'game_code' => $game->game_code,
            'response' => $response
        ]);

        return response()->json([
            'status' => false,
            'message' => $response['msg'] ?? 'Gagal membuka game'
        ]);

    } catch (\Throwable $e) {

        Log::critical('FIVER OPEN GAME EXCEPTION', [
            'user_id' => $user->id,
            'error' => $e->getMessage()
        ]);

        return response()->json([
            'status' => false,
            'message' => 'Terjadi kesalahan sistem'
        ], 500);
    }
}

}
