<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Config;

class PGGameConfigController extends Controller
{
    public function index()
    {
        $games = Config::get('games.gameName');
        $pgGames = [];

        foreach ($games as $gameId => $gameName) {
            if (substr(strval($gameId),0,2)=="90" ) {
                $config = Redis::get("GameConfigX_PG_{$gameId}");
                $pgGames[] = [
                    'gameId' => $gameId,
                    'gameName' => $gameName,
                    'config' => $config ? json_decode($config, true) : null
                ];
            }
        }

        return view('admin.pg_game_config.index', compact('pgGames'));
    }

    public function update(Request $request)
    {
        $gameId = $request->input('gameId');
        $config = $request->input('config');

        \Log::info('PG游戏配置更新请求', [
            'gameId' => $gameId,
            'config' => $config,
            'all_data' => $request->all()
        ]);

        if (!$gameId || !$config) {
            \Log::error('PG游戏配置更新失败：参数错误');
            return response()->json(['status' => 'error', 'message' => '参数错误']);
        }

        try {
            $configData = json_decode($config, true);
            if (json_last_error() !== JSON_ERROR_NONE) {
                \Log::error('PG游戏配置更新失败：JSON格式错误', ['error' => json_last_error_msg()]);
                throw new \Exception('JSON格式错误: ' . json_last_error_msg());
            }

            Redis::set("GameConfigX_PG_{$gameId}", $config);
            \Log::info('PG游戏配置更新成功', ['gameId' => $gameId]);
            return response()->json(['status' => 'success', 'message' => '更新成功']);
        } catch (\Exception $e) {
            \Log::error('PG游戏配置更新失败：异常', ['message' => $e->getMessage()]);
            return response()->json(['status' => 'error', 'message' => $e->getMessage()]);
        }
    }

    public function copyAll(Request $request)
    {
        $sourceGameId = (int) $request->input('gameId');
        $config = $request->input('config');

        \Log::info('PG游戏配置复制到所有游戏请求', [
            'sourceGameId' => $sourceGameId,
            'has_config' => $config !== null,
        ]);

        if (!$sourceGameId) {
            return response()->json(['status' => 'error', 'message' => '缺少gameId']);
        }

        try {
            if ($config) {
                $configData = json_decode($config, true);
                if (json_last_error() !== JSON_ERROR_NONE) {
                    throw new \Exception('JSON格式错误: ' . json_last_error_msg());
                }
            } else {
                $raw = Redis::get("GameConfigX_PG_{$sourceGameId}");
                if (!$raw) {
                    throw new \Exception('源游戏未找到配置');
                }
                $configData = json_decode($raw, true);
                if (json_last_error() !== JSON_ERROR_NONE) {
                    throw new \Exception('源配置JSON格式错误: ' . json_last_error_msg());
                }
            }

            $games = Config::get('games.gameName');
            $affected = 0;
            foreach ($games as $gameId => $gameName) {
                if ($gameId > 9000 && (int)$gameId !== $sourceGameId) {
                    Redis::set("GameConfigX_PG_{$gameId}", json_encode($configData, JSON_UNESCAPED_UNICODE));
                    $affected++;
                }
            }

            \Log::info('PG游戏配置复制完成', ['source' => $sourceGameId, 'affected' => $affected]);
            return response()->json(['status' => 'success', 'message' => '复制成功', 'affected' => $affected]);
        } catch (\Exception $e) {
            \Log::error('PG游戏配置复制失败', ['message' => $e->getMessage()]);
            return response()->json(['status' => 'error', 'message' => $e->getMessage()]);
        }
    }
}
