<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Betting;
use App\Models\GameId;
use App\Models\GamePoints;
use App\Models\GameSettings;
use App\Models\NextGameId;
use App\Models\PaymentSettings;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class AdminNextManagement extends Controller
{
    public function NextManagement()
    {
        $gameid = GameId::where(['status' => 0, 'type' => 'one_minute'])->with(['betting'])->latest()->first();
        $nextgameid = NextGameId::where(['status' => 0, 'type' => 'one_minute'])->latest()->get();
        $active_game = GameId::where(['type' => 'one_minute'])->latest()->first();
        $date_now = strtotime(date('Y-m-d H:i:s'));
        $last_game_time = getGameTime($active_game->created_at);

        return view('adminDashboard.next-management', compact('gameid', 'nextgameid', 'active_game', 'date_now', 'last_game_time'));
    }

    public function SetGameIdResult(Request $request)
    {
        $request->validate([
            'gameId' => 'required',
            'value' => 'required',
            'type' => 'required'
        ]);
        $code_type = $request->type . '_code';
        $exists = NextGameId::where(['game_id' => $request->gameId])->first();
        if ($exists) {
            if ($request->value == "00") {
                NextGameId::where(['game_id' => $request->gameId])->delete();
                return response()->json(['status' => 'success']);
            }
            $exists->$code_type = $request->value;
            $exists->save();
        } else {
            $nextgameid = new NextGameId();
            $nextgameid->game_id = $request->gameId;
            $nextgameid->$code_type = $request->value;
            $nextgameid->type = $request->type;
            $nextgameid->save();
        }
        return response()->json(['status' => 'success']);
    }

    public function NextManagementRecords(Request $request)
    {
        $request->validate([
            'tab' => 'required'
        ]);

        $nextgameid = NextGameId::where(['status' => 0, 'type' => $request->tab])->get();
        $active_game = GameId::where(['type' => $request->tab])->latest()->first();

        $time_interval = 1;
        $game_type_code = "one_minute_code";
        $counter = 180;
        if ($request->tab == "three_minute") {
            $time_interval = 3;
            $game_type_code = "three_minute_code";
            $counter = 60;
        } else if ($request->tab == "five_minute") {
            $time_interval = 5;
            $game_type_code = "five_minute_code";
            $counter = 36;
        } else if ($request->tab == "two_minute") {
            $time_interval = 2;
            $game_type_code = "two_minute_code";
            $counter = 90;
        }

        $date_now = strtotime(date('Y-m-d H:i:s'));
        $last_game_time = getGameTime($active_game->created_at, $time_interval);

        $nextGamesData = [];
        for ($i = $active_game->game_id + 1; $i <= $active_game->game_id + $counter; $i++) {
            $data['game_id'] = $i;
            $time = Date("H:i:s d-m-Y", strtotime(($i - $active_game->game_id) * $time_interval . " minute", strtotime($active_game->created_at)));
            $data['created_at'] = $time;
            $data['selectedValue'] = null;
            foreach ($nextgameid as $nextGame) {
                if ($data['selectedValue'] == null && $nextGame->game_id == $i) {
                    $data['selectedValue'] = $nextGame->$game_type_code;
                }
            }
            array_push($nextGamesData, $data);
        }

        return response()->json(['status' => 'success', 'gameid' => $active_game, 'date_now' => $date_now, 'last_game_time' => $last_game_time, 'nextGamesData' => $nextGamesData]);
    }
}
