<?php

namespace App\Http\Controllers\Admin;

use App\Facade\TableName;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class ProtectController
{
    // 保护配置列表
    public function protect_config_info()
    {
        $list = DB::connection('write')->table(TableName::QPPlatformDB() . 'ProtectConfigInfo')->get();

        return view('admin.protect.protect_config_info', compact('list'));
    }

    // 保护配置修改
    public function protect_config_info_update(Request $request, $ID)
    {
        if ($request->isMethod('post')) {
            $post = $request->post();
            $post = array_map(function ($val) {
                return (int)$val;
            }, $post);
            
            DB::connection('write')->table(TableName::QPPlatformDB() . 'ProtectConfigInfo')
                ->where('ID', $ID)
                ->update($post);
            return apiReturnSuc();
        } else {
            $info = DB::connection('write')->table(TableName::QPPlatformDB() . 'ProtectConfigInfo')->where('ID', $ID)->first();
            return view('admin.protect.protect_config_info_update', compact('info'));
        }
    }

    // 免费保护配置
    public function free_protect_config()
    {
        $configData = Redis::get("FreeProtectConfig");
        $config = $configData ? json_decode($configData, true) : [
            "ProtectScore" => 3,
            "ScoreBetRatio" => 3,
            "ScoreBet" => 10,
            "ProtectRounds" => [40, 100],
            "ProtectRoundsLoop" => 100,
            "ProtectRoundsRatio" => 10
        ];

        return view('admin.protect.free_protect_config', compact('config'));
    }

    // 免费保护配置修改
    public function free_protect_config_update(Request $request)
    {
        if ($request->isMethod('post')) {
            $config = $request->input('config');

            \Log::info('免费保护配置更新请求', [
                'config' => $config,
                'all_data' => $request->all()
            ]);

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

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

                Redis::set("FreeProtectConfig", $config);
                \Log::info('免费保护配置更新成功');
                return response()->json(['status' => 'success', 'message' => '更新成功']);
            } catch (\Exception $e) {
                \Log::error('免费保护配置更新失败：异常', ['message' => $e->getMessage()]);
                return response()->json(['status' => 'error', 'message' => $e->getMessage()]);
            }
        } else {
            $configData = Redis::get("FreeProtectConfig");
            $config = $configData ? json_decode($configData, true) : [
                "ProtectScore" => 3,
                "ScoreBetRatio" => 3,
                "ScoreBet" => 10,
                "ProtectRounds" => [40, 100],
                "ProtectRoundsLoop" => 100,
                "ProtectRoundsRatio" => 10
            ];
            return view('admin.protect.free_protect_config_update', compact('config'));
        }
    }
}
